我已经尝试在我的sqlite数据库中导出函数将其导出到Excel工作表。但我不知道我的导出函数是否成功实现,如果是,那么我导出的文件在哪里,如果我的代码包含一些错误,请帮助我out becoz我是android.this的新手是我的sqlite控制器类,包括导出功能: -
public class SQLController {
private DBhelper dbhelper;
private Context ourcontext;
private SQLiteDatabase database;
public SQLController(Context c) {
ourcontext = c;
}
public SQLController open() throws SQLException {
dbhelper = new DBhelper(ourcontext);
database = dbhelper.getWritableDatabase();
return this;
}
public void close() {
dbhelper.close();
}
public void insertData(String name) {
ContentValues cv = new ContentValues();
cv.put(DBhelper.MEMBER_NAME, name);
database.insert(DBhelper.TABLE_MEMBER, null, cv);
}
public Cursor readData() {
String[] allColumns = new String[] { DBhelper.MEMBER_ID,
DBhelper.MEMBER_NAME };
Cursor c = database.query(DBhelper.TABLE_MEMBER, allColumns, null,
null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
public int updateData(long memberID, String memberName) {
ContentValues cvUpdate = new ContentValues();
cvUpdate.put(DBhelper.MEMBER_NAME, memberName);
int i = database.update(DBhelper.TABLE_MEMBER, cvUpdate,
DBhelper.MEMBER_ID + " = " + memberID, null);
return i;
}
public void deleteData(long memberID) {
database.delete(DBhelper.TABLE_MEMBER, DBhelper.MEMBER_ID + "="
+ memberID, null);
}
public void export() throws IOException{
File dbFile=ourcontext.getDatabasePath(dbhelper. DB_NAME);
File exportDir = new File(Environment.getExternalStorageDirectory()+"/XLS", "");
if (!exportDir.exists())
{
Log.d("export", ""+exportDir);
exportDir.mkdirs();
}
File file = new File(exportDir, "Your file.xls");
file.createNewFile();
File file8 = null;
CSVWriter csvWrite8 = new CSVWriter(new FileWriter(file8));
String[] allColumns = new String[] { DBhelper.MEMBER_ID,
DBhelper.MEMBER_NAME };
Cursor c = database.query(DBhelper.TABLE_MEMBER, allColumns, null,
null, null, null, null);
CSVWriter csvWrite = null;
if(c.moveToFirst())
{
c.moveToFirst();
Log.d("log is","No of Column is ---->>>"+c.getColumnNames().length);
do
{
String arrStr[] = {c.getString(0),c.getString(1),c.getString(2),c.getString(3),c.getString(4),c.getString(5)};
csvWrite.writeNext(arrStr);
}while(c.moveToNext());
}
csvWrite.close();
}
}// outer class end
这是我的db帮助程序类
public class DBhelper extends SQLiteOpenHelper {
// TABLE INFORMATTION
public static final String TABLE_MEMBER = "member";
public static final String MEMBER_ID = "_id";
public static final String MEMBER_NAME = "name";
// DATABASE INFORMATION
static final String DB_NAME = "MEMBER.DB";
static final int DB_VERSION = 1;
// TABLE CREATION STATEMENT
private static final String CREATE_TABLE = "create table "
+ TABLE_MEMBER + "(" + MEMBER_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ MEMBER_NAME + " TEXT NOT NULL);";
public DBhelper(Context context) {
super(context, DB_NAME, null,DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + TABLE_MEMBER);
onCreate(db);
}
}
这是我的类,我在按钮id case中调用export函数
public class Modify_member extends Activity implements OnClickListener {
EditText et;
Button edit_bt, delete_bt,bt;
long member_id;
SQLController dbcon;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.modify_member);
dbcon = new SQLController(this);
dbcon.open();
et = (EditText) findViewById(R.id.edit_mem_id);
edit_bt = (Button) findViewById(R.id.update_bt_id);
delete_bt = (Button) findViewById(R.id.delete_bt_id);
bt=(Button) findViewById(R.id.ex);
Intent i = getIntent();
String memberID = i.getStringExtra("memberID");
String memberName = i.getStringExtra("memberName");
member_id = Long.parseLong(memberID);
et.setText(memberName);
edit_bt.setOnClickListener(this);
delete_bt.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.update_bt_id:
String memName_upd = et.getText().toString();
dbcon.updateData(member_id, memName_upd);
this.returnHome();
break;
case R.id.delete_bt_id:
dbcon.deleteData(member_id);
this.returnHome();
break;
case R.id.ex:
try {
dbcon.export();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.returnHome();
break;
}}
public void returnHome() {
Intent home_intent = new Intent(getApplicationContext(),
f.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(home_intent);
}
}
答案 0 :(得分:0)
您的代码表明:文件夹是您的" exportDir",文件名是"您的file.xls"。
尝试查找您的文件。但请确保在此之前实际生成该文件。