2013-03-15 131 views
3

「我遇到錯誤‘沒有這樣的表’,並導致我嘗試一切可能,使之消失,現在它走了,但我很迷惑,這裏是我的情況SQLite數據庫的Android創建表

我有2個我需要在應用程序中創建表,並且每個表放置一個類,它看起來像下面的代碼。如果我遵循這個方法,當我從「表2」中提取數據時,會觸發「沒有這樣的表」,所以我需要更改DATABASE_NAME到其他值=「DB2」;然後「Table2」將創建成功並能夠提取數據。

所以我的問題是這是一個正確的方法來確保2表創建成功?或者我不應該單獨的表分爲2類?請告知

public class DB1 extends SQLiteOpenHelper { 
// Database Version 
private static final int DATABASE_VERSION = 1; 

// Database Name 
private static final String DATABASE_NAME = "DB"; 

// Labels table name 
private static final String TABLE_STORE = "Table1"; 

// Labels Table Columns names 
public static final String KEY_1 = "col1"; 

public RC_StoreTbl(Context context) { 
    super(context, DATABASE_NAME, null, DATABASE_VERSION); 
} 

// Creating Tables 
@Override 
public void onCreate(SQLiteDatabase db) { 
    // Category table create query 
    String CREATE_TABLE = "CREATE TABLE " + TABLE_STORE + "(" 
      +KEY_1 + " INTEGER PRIMARY KEY)"; 
    db.execSQL(CREATE_TABLE); 
} 
} 

另一類

public class DB2 extends SQLiteOpenHelper { 
// Database Version 
private static final int DATABASE_VERSION = 1; 

// Database Name 
private static final String DATABASE_NAME = "DB"; 

// Labels table name 
private static final String TABLE_STORE = "Table2"; 

// Labels Table Columns names 
public static final String KEY_1 = "col1"; 

public RC_StoreTbl(Context context) { 
    super(context, DATABASE_NAME, null, DATABASE_VERSION); 
} 

// Creating Tables 
@Override 
public void onCreate(SQLiteDatabase db) { 
    // Category table create query 
    String CREATE_TABLE = "CREATE TABLE " + TABLE_STORE + "(" 
      +KEY_1 + " INTEGER PRIMARY KEY)"; 
    db.execSQL(CREATE_TABLE); 
} 
} 
+0

對不起。但爲什麼你需要兩個單獨的類爲兩個表?這是不是也創造了兩個數據庫? – 2013-03-15 03:54:52

+0

對不起,我想我被誤解了這個概念,我認爲它會把它當作同一個數據庫,並且創建不存在的表,我嘗試使它更簡潔,更容易被桌/類維護。 – tsohtan 2013-03-15 04:21:52

+1

我想你應該給這個教程一個閱讀。這將爲你清理一些事情。 http://www.vogella.com/articles/AndroidSQLite/article.html – 2013-03-15 04:23:50

回答

4

不,你應該把表中的一個數據庫類。只要確保無論何時您更改代碼以修改表格或添加/刪除表格以清除數據或卸載應用並再次運行。你最有可能得到「沒有這樣的表」錯誤,因爲你添加或修改表,在這種情況下,onCreate將不會被再次調用,直到你清除數據或卸載你的應用程序。

+0

我與上面的答案一起去。只需添加一點,就可以使用單個數據庫處理程序類並在其中創建任意兩個或更多表。只需確保卸載(或清除應用程序數據),以便每次更改數據庫結構,因爲onCreate只會被調用一次,即在安裝應用程序期間。 – SKK 2013-03-15 05:00:48

1

不,你應該有一個類(通常我們命名爲DBopenHelper),它擴展了SQLiteOpenHelper,在那個類中你必須操縱表的創建。

所以你的代碼如下所示:

public class DBOpenHelper extends SQLiteOpenHelper { 

     public static final String DATABASE_NAME = "DB"; 


      // Update the DATABASE_VERSION so that onUpgrade can be executed! 
     public static final int DATABASE_VERSION = 2;    

     // Labels table name 
     private static final String TABLE_STORE1 = "Table1"; 

     // Labels Table Columns names 
     public static final String TABLE1_KEY_1 = "Tcol1"; 

      // Labels table name 
      private static final String TABLE_STORE2 = "Table2"; 

      // Labels Table Columns names 
      public static final String TABLE2_KEY_1 = "Tcol1";    

     public RC_StoreTbl(Context context) { 
       super(context, DATABASE_NAME, null, DATABASE_VERSION); 
      } 

     @Override 
     public void onCreate(SQLiteDatabase db) { 
      if (!db.isReadOnly()) { 
       // Enable foreign key constraints 
       db.execSQL("PRAGMA foreign_keys = ON;"); 
       Log.i("TAG", "FOREIGN KEY constraint enabled!"); 
      }  

      // table create query 
       String CREATE_TABLE = "CREATE TABLE " + TABLE_STORE1 + "(" 
         +TABLE1_KEY_1 + " INTEGER PRIMARY KEY)"; 

      // table create query 
       String CREATE_TABLE = "CREATE TABLE " + TABLE_STORE2 + "(" 
         +TABLE2_KEY_1 + " INTEGER PRIMARY KEY AUTOINCREMENT)"; 

// table create query 
       String CREATE_TABLE = "CREATE TABLE " + TABLE_STORE + "(" 
         +KEY_1 + " INTEGER PRIMARY KEY AUTOINCREMENT)"; 


    // ITS ALWAYS GOOD TO PUT execSQL in the try catch block for tracking // PROBLEMS/ERRORS/EXCEPTIONS 
     try { 
        db.execSQL(CREATE_TABLE); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 

      } // end onCrate 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
      Log.i(LOG_TAG, "Upgrading database from " + oldVersion + " to " 
        + newVersion); 
      // kill previous tables 
      db.execSQL("DROP TABLE IF EXISTS " + TABLE_STORE1); 
      db.execSQL("DROP TABLE IF EXISTS " + TABLE_STORE2); 
      onCreate(db); 
     } // end onUpgrage 

    } 
0

因爲DB存在,您不容創建第二個表,只有TE先上創建可以創建表嘗試這樣的事情

public class DB1 extends SQLiteOpenHelper { 
// Database Version 
     private static final int DATABASE_VERSION = 1; 

// Database Name 
     private static final String DATABASE_NAME = "DB"; 

// Labels table name 
     private static final String TABLE_STORE1 = "Table1"; 
     private static final String TABLE_STORE2 = "Table2"; 

// Labels Table Columns names 
     public static final String KEY_1 = "col1"; 

//first query 
    String firstTable="CREATE TABLE " + TABLE_STORE1 + "(" 
     +KEY_1 + " INTEGER PRIMARY KEY)"; 
//second query 

    String secondTable="CREATE TABLE " + TABLE_STORE2 + "(" 
     +KEY_1 + " INTEGER PRIMARY KEY)"; 

    public RC_StoreTbl(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

// Creating Tables 
@Override 
public void onCreate(SQLiteDatabase db) { 

db.execSQL(firstTable); 
db.execSQL(secondTable); 
} 
} 

sory for my english XD

0

你應該考慮改變你的表模型。這將幫助你更好地組織事情,並儘早發現錯誤。你可以找到一個例子here

下面是一個表模型的例子。

public class ProductTable implements BaseColumns { 
    public static final String NAME = "name"; 
    public static final String PRICE = "price"; 
    public static final String TABLE_NAME = "products"; 

    public static final String CREATE_QUERY = "create table " + TABLE_NAME + " (" + 
     _ID + " INTEGER, " + 
     NAME + " TEXT, " + 
     PRICE + " INTEGER)"; 

    public static final String DROP_QUERY = "drop table " + TABLE_NAME; 
    public static final String SElECT_QUERY = "select * from " + TABLE_NAME; 
} 

現在在DatabaseHelper中使用下面的代碼創建表。

@Override 
    public void onCreate(SQLiteDatabase sqLiteDatabase) { 
    sqLiteDatabase.execSQL(ProductTable.CREATE_QUERY); 
    seedProducts(sqLiteDatabase); 
    }