java - SQLite仅创建2个表

标签 java android sql sqlite

由于某些原因,SQLite仅创建两个表而不是4。我正在使用以下代码:

@Override
public void onCreate(SQLiteDatabase db) 
{
    String TABLE_RECEIPT            = "CREATE TABLE RECEIPT (receiptId INTEGER PRIMARY KEY AUTOINCREMENT, receiptDate INTEGER); ";
    db.execSQL(TABLE_RECEIPT);

    String TABLE_PRODUCT            = "CREATE TABLE PRODUCT (productId INTEGER PRIMARY KEY AUTOINCREMENT, productName TEXT NOT NULL, productImagePath TEXT, productPrice REAL NOT NULL);";
    db.execSQL(TABLE_PRODUCT);

    String TABLE_RECEIPT_RULE       = "CREATE TABLE RECEIPT_RULE (receiptId INTEGER NOT NULL, productId INTEGER NOT NULL, ruleAmount INTEGER NOT NULL, PRIMARY KEY (receiptId, productId), FOREIGN KEY(receiptId) REFERENCES RECEIPT(receiptId), FOREIGN KEY(productId) REFERENCES PRODUCT(productId));";
    db.execSQL(TABLE_RECEIPT_RULE);

    String TABLE_PRODUCT_ATTRIBUTES = "CREATE TABLE PRODUCT_ATTRIBUTES (productId INTEGER NOT NULL, attributeName TEXT NOT NULL, attributeValue TEXT NOT NULL, PRIMARY KEY (productId, attributeName), FOREIGN KEY(productId) REFERENCES PRODUCT(productId));";
    db.execSQL(TABLE_PRODUCT_ATTRIBUTES);

    Log.i(Config.TAG, "Recreated database");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
{
    String TABLE_PRODUCT            = "DROP TABLE PRODUCT;";
    String TABLE_RECEIPT            = "DROP TABLE RECEIPT;";
    String TABLE_RECEIPT_RULE       = "DROP TABLE RECEIPT_RULE;";
    String TABLE_PRODUCT_ATTRIBUTES = "DROP TABLE PRODUCT_ATTRIBUTES;";

    db.execSQL(TABLE_PRODUCT);
    db.execSQL(TABLE_RECEIPT);
    db.execSQL(TABLE_RECEIPT_RULE);
    db.execSQL(TABLE_PRODUCT_ATTRIBUTES);
    onCreate(db);
}


消息Recreated database是可见的,因此它会尝试创建所有表。它没有显示任何类型的错误。

我不知道它是否有用,但是我正在读取具有以下代码的现有表:

//Placed in SQLiteStorage
public Cursor showAllTables(){
    String mySql = " SELECT name FROM sqlite_master " + " WHERE type='table'             "
            + "   AND name LIKE 'PR_%' ";
    return db.rawQuery(mySql, null);
}   

//In Activity, create the storage and calls the function 
Cursor c = new SQLiteStorage(this).showAllTables();
if (c.moveToFirst()) 
{
    while(!c.isAfterLast())
    {
        Log.i(Config.TAG, c.getString(0));
        c.moveToNext();
    }
}

最佳答案

在showAllTable函数中,您选择了所有以PR开头的表,因此查询中仅显示产品表和产品属性表。

更改

字符串mySql =“从sqlite_master选择名称” +“ WHERE type ='table'”
            +“ AND名称,例如'PR_%'”;



字符串mySql =“从sqlite_master选择名称” +“ WHERE type ='table'”;

关于java - SQLite仅创建2个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26474447/

相关文章:

java - 我可以告诉安卓系统当前没有插入耳机吗?

java - 适用于Exoplayer的Ooyala SDK-NoClassDefFoundError:com.ooyala.android.player.exoplayer.ExoStreamPlayer

java - 有没有办法从多重映射中的值获取所有键?

sql - SQL Server计数查询无顺序

sql - 使用 postgresql 从多个表中选择相同的字段

java - fatal error -序言中不允许包含内容

android - Android SDK Canary 4中Gradle构建失败

Android:以编程方式将文件从 sdcard 复制到应用程序的 Assets 文件夹

Android 位置感知通知

sql - 在 sql server 中修剪左边的字符?