问题使用SQLite的onCreate()在Android中问题、SQLite、Android、onCreate

2023-09-06 22:08:25 作者:到最后忘了自己

我在扩展SQLiteOpenHelper类来帮助我连接,做我的数据库工作。按照文档,onCreate方法如果数据库尚未创建只应被调用。然而,我的问题是,当我试图执行一个查询插入一条记录,我收到此错误。

I'm extending the SQLiteOpenHelper class to help me connect and do my database work. According to the documentation, the OnCreate method should only be called if the database has not been created. Yet, my problem is that I am getting this error when I try to execute a query to insert a record.

ERROR/Database(214): Failure 1 (table Sample already exists) on 0x218688 when preparing 
'CREATE TABLE Sample (RecId INT, SampleDesc TEXT);'.

此创建查询的唯一地方是在code采用的是看起来像这样的onCreate方法。

The only place this Create query is used in code is the OnCreate method which looks like this.

 @Override
      public void onCreate(SQLiteDatabase db) {
          db.execSQL(SAMPLE_TABLE_CREATE);
      }

注意:我跟随教程 - 我已经做了不同的唯一事情就是让SQLiteDatabase对象公开,而​​不是私有,这样我可以扩展这个类为每个实体,从继承DataHelper类让公众SQLiteDatabase对象完成所有的工作。

Note: I'm following a tutorial - the only thing I've done different is make the SQLiteDatabase object public instead of private so that I could extend this class for each entity, letting the public SQLiteDatabase object from the inherited DataHelper class do all the work

下面是使失败呼叫的方法。

Here is the method that makes the call that fails.

 //This method is in the class that extends DataHelper (See note on tutorial)
    public void createSample(Sample sample)//next action form
    {           
            String id =  sample.getId();
            String name =  sample.getSummary();

            String query = "INSERT INTO " + SAMPLE_TABLE_NAME + "( " + SAMPLE_Id + "," + 
                        SAMPLE_NAME + ") " + " VALUES (" + id + "," + name + ")";

            try{        
                 data.rawQuery(query, null);    
            }
            catch(SQLException e){
                 Log.i("Sample", "Errors: Sample LN60: " + e.getMessage());   
            }
    }

谁能告诉我什么,我做错了什么?或者,也许一个黑客(即检查表执行create语句之前存在)

Can someone tell me what I'm doing wrong? Or maybe a hack (i.e. check if table exists before executing create statement)

请让我知道我可以张贴什么其他的code来解决这个...

Please let me know what other code I can post to solve this...

推荐答案

是不是因为你已经执行一次它的活动,从不破坏DB之后呢?和2来看,你会遇到这个错误。

Is it due to you've execute it your activity once and never destroy the DB after that? And 2nd run you'd hit this error.

数据库存储在 /数据/数据​​/ YOUR_PACKAGE /数据库/ ,这样一个解决方法是检查数据库创建它之前就存在这里。

Database is stored in /data/data/YOUR_PACKAGE/databases/, so a workaround would be to check if the DB exists here before creating it.

    //The Android's default system path of your application database.
    private static String DB_PATH = "/data/data/YOUR_PACKAGE/databases/";
    private static String DB_NAME = "myDBName";

    SQLiteDatabase checkDB = null;

    String myPath = DB_PATH + DB_NAME;
    checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
    if(checkDB){
      //do nothing
    }else{
      //create DB
    }

code源这里