我尝试将数据插入数据库SQLLite,但数据不会进入数据库:
ContentValues cv = new ContentValues();
cv.put("username", cust.getUsername());
cv.put("password", cust.getPassword());
cv.put("name", cust.getCustomername());
long id = mDb.insert("user", null, cv);
Log.v(TAG, Long.toString(id));
Log.e(TAG, "New Customer Created");
return true;
id返回5,任何想法为什么? (几次后插入id返回7。)
我应该在这里发布哪些其他代码?数据库连接应该没问题,因为我能够成功登录。
答案 0 :(得分:1)
正如您所提到的,您的数据库存储在资产文件夹中。资产或资源中的每个文件都是只读的。您必须将数据库文件复制到另一个路径以将数据写入其中。
以下是SQLiteOpenHelper
的一些代码:
public static class DBHelper extends SQLiteOpenHelper{
private static final String DATABASE_NAME = "database.db";
private static final int DATABASE_VERSION = 1;
private static final String ASSET_DB_DIRECTORY = "db";
private static final String DATABASE_RESOURCE_PATH = ASSET_DB_DIRECTORY + File.separator + DATABASE_NAME;
private final Context context;
private SQLiteDatabase db;
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void createDatabase() throws IOException{
boolean dbExist = checkDatabase();
if(dbExist){
//do nothing - database already exist
}else{
//By calling this method and empty database will be created into the default system path
//of your application so we are gonna be able to overwrite that database with our database.
this.getReadableDatabase();
try {
copyDatabase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
public boolean checkDatabase(){
SQLiteDatabase checkDB = null;
File file = context.getDatabasePath(DATABASE_NAME);
try {
checkDB = SQLiteDatabase.openDatabase(file.getPath(), null, SQLiteDatabase.OPEN_READONLY | SQLiteDatabase.NO_LOCALIZED_COLLATORS);
} catch (Exception e) {
//database does't exist yet.
}
if(checkDB != null){
checkDB.close();
if(file.lastModified() != new File(DATABASE_RESOURCE_PATH).lastModified())
return false;
}
return checkDB != null;
}
private void copyDatabase() throws IOException{
//Open your local db as the input stream
InputStream myInput = context.getAssets().open(DATABASE_RESOURCE_PATH);
// Path to the just created empty db
String outFileName = context.getDatabasePath(DATABASE_NAME).getPath();
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public SQLiteDatabase openDataBase() throws SQLException{
//Open the database
String myPath = context.getDatabasePath(DATABASE_NAME).getPath();
db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY | SQLiteDatabase.NO_LOCALIZED_COLLATORS);
return db;
}
答案 1 :(得分:0)
检查从模拟器真正插入db下载数据库文件的内容。您可以使用Android Debug Monitor
然后在任何SQLite客户端中打开数据库