3

“我收到错误“没有这样的表”并导致我尽一切可能让它消失,现在它消失了,但我很困惑。这是我的情况

我有 2 个表需要在应用程序中创建。我每张桌子放一个班级,它看起来像下面的代码。如果我遵循这个,当我从“Table2”中提取数据时,它会用“No Such Table”打击我,所以我需要将 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);
}
}
4

4 回答 4

5

不,您应该将表放在一个数据库类中。只需确保每当您更改代码以修改表格或添加/删除表格时,您都会清除数据或卸载应用程序并再次运行。您很可能会收到“没有这样的表”错误,因为您添加或修改了一个表,在这种情况下,在您清除数据或卸载您的应用程序之前不会再次调用onCreate 。

于 2013-03-15T03:57:00.513 回答
2

不,您应该有一个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

    }
于 2013-03-15T05:09:46.650 回答
0

你不能创建第二个表,因为数据库存在,只有先创建才能创建表尝试这样的事情

 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);
}
}

对不起我的英语XD

于 2015-05-18T16:04:50.543 回答
0

您应该考虑更改表格的模型。这将帮助您更好地组织事情并尽早发现错误。你可以在这里找到一个例子。

下面是一个表格模型的例子。

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);
  }
于 2015-08-23T07:03:39.593 回答