3

我试图将数据插入数据库 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 return 7 现在。)

我应该在这里发布什么其他代码?数据库连接应该没问题,因为我能够成功登录。

4

2 回答 2

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;
    }
于 2013-07-10T08:16:28.003 回答
0

检查从模拟器中真正插入 db 下载数据库文件的内容。您可以使用 Android 调试监视器

安卓调试监视器

然后在任何 SQLite 客户端中打开数据库

于 2013-07-10T11:28:49.933 回答