我正在尝试将数据从一个活动导出到一个&csv'我的数据库类(DBHelper.java)中的文件,但收到以下错误:
no such table: SQLiteExample.db (Sqlite code 1): , while compiling: SELECT * FROM SQLiteExample.db, (OS error - 2:No such file or directory)
android.database.sqlite.SQLiteException: no such table: SQLiteExample.db (Sqlite code 1): , while compiling: SELECT * FROM SQLiteExample.db, (OS error - 2:No such file or directory)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
这是我的报告活动代码(ExportJobCard.java)
public void exportDataBaseIntoCSV() {
DBHelper db = new DBHelper(this);
File exportDir = new File(Environment.getExternalStorageDirectory(), "/JOBCARDSDATABASE");
if (!exportDir.exists()) {
exportDir.mkdirs();
}
File file = new File(exportDir, "csvfilename.csv");
try {
file.createNewFile();
CSVWriter csvWrite = new CSVWriter(new FileWriter(file));
SQLiteDatabase sql_db = db.getReadableDatabase();
Cursor curCSV = sql_db.rawQuery("SELECT * FROM " + DBHelper.DATABASE_NAME, null);
csvWrite.writeNext(curCSV.getColumnNames());
while (curCSV.moveToNext()) {
//Which column you want to export you can add over here...
String arrStr[] = {curCSV.getString(0), curCSV.getString(1), curCSV.getString(2)};
csvWrite.writeNext(arrStr);
}
csvWrite.close();
curCSV.close();
} catch (Exception sqlEx) {
Log.e("Error:", sqlEx.getMessage(), sqlEx);
}
}
如果我直接在我的JobCard.class中添加上面的代码,用户输入数据并点击保存它可以工作,但我希望报告有不同的活动,因为我有2个数据库,我需要获取报告从
我不确定我在这里做错了什么,因为如果从上面提到的活动起作用的话,如果我只是从另一个活动那里做它肯定会起作用吗?
这是我的数据库类(DBHelper.java)
public class DBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "SQLiteExample.db";
private static final int DATABASE_VERSION = 2;
public static final String PERSON_TABLE_NAME = "person";
public static final String PERSON_COLUMN_ID = "_id";
public static final String PERSON_COLUMN_NAME = "name";
public static final String PERSON_COLUMN_GENDER = "gender";
public static final String PERSON_COLUMN_AGE = "age";
public DBHelper(Context context) {
super(context, DATABASE_NAME , null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(
"CREATE TABLE " + PERSON_TABLE_NAME +
"(" + PERSON_COLUMN_ID + " INTEGER PRIMARY KEY, " +
PERSON_COLUMN_NAME + " TEXT, " +
PERSON_COLUMN_GENDER + " TEXT, " +
PERSON_COLUMN_AGE + " INTEGER)"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + PERSON_TABLE_NAME);
onCreate(db);
}
public boolean insertPerson(String name,
String gender,
int age) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(PERSON_COLUMN_NAME, name);
contentValues.put(PERSON_COLUMN_GENDER, gender);
contentValues.put(PERSON_COLUMN_AGE, age);
db.insert(PERSON_TABLE_NAME, null, contentValues);
return true;
}
public int numberOfRows() {
SQLiteDatabase db = this.getReadableDatabase();
int numRows = (int) DatabaseUtils.queryNumEntries(db, PERSON_TABLE_NAME);
return numRows;
}
public boolean updatePerson(Integer id,
String name,
String gender,
int age) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(PERSON_COLUMN_NAME, name);
contentValues.put(PERSON_COLUMN_GENDER, gender);
contentValues.put(PERSON_COLUMN_AGE, age);
contentValues.put(PERSON_JOBDESCRIPTION, jobdescription);
db.update(PERSON_TABLE_NAME, contentValues, PERSON_COLUMN_ID + " = ? ", new String[] { Integer.toString(id) } );
return true;
}
public Integer deletePerson(Integer id) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(PERSON_TABLE_NAME,
PERSON_COLUMN_ID + " = ? ",
new String[] { Integer.toString(id) });
}
public Cursor getPerson(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("SELECT * FROM " + PERSON_TABLE_NAME + " WHERE " +
PERSON_COLUMN_ID + "=?", new String[]{Integer.toString(id)});
return res;
}
public Cursor getAllPersons() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "SELECT * FROM " + PERSON_TABLE_NAME, null );
return res;
}
}
有人可以帮我解决这个问题吗?
答案 0 :(得分:1)
你的问题可能来自不同的问题。我之前面对它并且我试图帮助你
当你创建一个sqlite数据库时,你应该将它注册到清单文件中,但是你再次面对它卸载并重新安装你的应用程序,因为你已经为数据库的下一个版本创建了数据库,例如新表,因为数据库已经存在你的调试器不会创建新的调试器或更换旧的调试器,所以虽然你编辑了数据库,但手机中已经存在的东西是旧的。 所以卸载并重新安装你的应用程序,以便创建新的sqlite数据库。 如果您再次遇到此问题,请检查您的语法,这绝对是您的语法错误。 而且我认为另一个问题在这里
"CREATE TABLE " + PERSON_TABLE_NAME +
"(" + PERSON_COLUMN_ID + " INTEGER PRIMARY KEY, " +
PERSON_COLUMN_NAME + " TEXT, " +
PERSON_COLUMN_GENDER + " TEXT, " +
PERSON_COLUMN_AGE + " INTEGER)"
替换
"CREATE TABLE " + PERSON_TABLE_NAME +
"(" + PERSON_COLUMN_ID + " INTEGER PRIMARY KEY, " +
PERSON_COLUMN_NAME + " TEXT, " +
PERSON_COLUMN_GENDER + " TEXT, " +
PERSON_COLUMN_AGE + " INTEGER);"
你忘记添加“;”
希望我能帮到你。