代码之家  ›  专栏  ›  技术社区  ›  Tanmay Singal

无法在Android中查询数据库

  •  2
  • Tanmay Singal  · 技术社区  · 8 年前

    我正在尝试制作购物应用程序的购物车,其中我首先查询购物车元素,然后从购物车的id中,列表元素通过查询另一个包含产品信息的表来查找与产品相关的相应元数据。我可以 查询产品列表成功 在显示“菜单”时,我试图将相同的代码应用于购物车。但它告诉我 列“\u id”不存在 。我已经坚持了一段时间了。

    You can find the entire project on GitHub

    以下是相关文件的重要部分

    你的购物车。Java语言

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_your_cart);
    
        ContentValues values = new ContentValues();
    
        values.put(CartContract.CartEntry._ID, 2);
        values.put(CartContract.CartEntry.COLUMN_NAME_ORDERED_QUANTITY, 37);
    
        getContentResolver().insert(CartContract.CartEntry.CONTENT_URI, values);
    
        String[] projection = {
    
                CartContract.CartEntry._ID,
                CartContract.CartEntry.COLUMN_NAME_ORDERED_QUANTITY
    
        };
    
        //gets the entire cart
        cart = getContentResolver().query(CartContract.CartEntry.CONTENT_URI, projection, null, null, null);
    
        ListView cartList = findViewById(R.id.CartListView);
        cartList.setAdapter(new cartAdapter(YourCart.this, cart));
    
    }
    
    // Following is part of cartAdapter
    @Override
        public void bindView(View view, Context context, Cursor cart) {
    
    
            prodName = view.findViewById(R.id.cartListElementProductNameTextView);
            prodPrice = view.findViewById(R.id.cartListElementProductPriceTextView);
    
            //Projection is just the name of the columns we would like to receive
            String[] projection = {
    
                    ProductListContract.ProductEntry.COLUMN_NAME_PRODUCT_THUMBNAIL,
                    ProductListContract.ProductEntry.COLUMN_NAME_PRODUCT_NAME,
                    ProductListContract.ProductEntry.COLUMN_NAME_PRODUCT_PRICE
    
            };
    
            Integer ui = cart.getInt(cart.getColumnIndexOrThrow(CartContract.CartEntry._ID));
    
            String[] hoho = {ui.toString()};
    
            Cursor productCursor = getContentResolver().query(ProductListContract.ProductEntry.CONTENT_URI, projection, ProductListContract.ProductEntry._ID, hoho, null);
    
            prodName.setText(productCursor.getInt(productCursor.getColumnIndexOrThrow(ProductListContract.ProductEntry.COLUMN_NAME_PRODUCT_NAME)));
    
            ui = productCursor.getInt(productCursor.getColumnIndexOrThrow(ProductListContract.ProductEntry.COLUMN_NAME_PRODUCT_PRICE));
            prodPrice.setText(ui.toString());
    
            productCursor.close();
    
        }
    

    我敢肯定,在创建表时会创建列,这可以从数据库助手的摘录中看到

     public static final String SQL_CREATE_ENTRIES =
            "CREATE TABLE " + TABLE_NAME + " ( " +
                    _ID + " INTEGER NON NULL, " +
                    COLUMN_NAME_ORDERED_QUANTITY + " INTEGER)";
    

    最后,这里是这次坠机的记录。一旦启动YourCart活动,应用程序就会崩溃

        03-16 09:50:30.987 11672-11672/com.example.tanmay.shoppingapp E/SQLiteLog: (1) table cart has no column named _id
    03-16 09:50:30.991 11672-11672/com.example.tanmay.shoppingapp E/SQLiteDatabase: Error inserting quantity=37 _id=2
                                                                                    android.database.sqlite.SQLiteException: table cart has no column named _id (code 1): , while compiling: INSERT INTO cart(quantity,_id) VALUES (?,?)
                                                                                        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                                        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
                                                                                        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
                                                                                        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                                        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                                                                                        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
                                                                                        at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1472)
                                                                                        at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1343)
                                                                                        at com.example.tanmay.shoppingapp.DataSet.DataProvider.insertCart(DataProvider.java:169)
                                                                                        at com.example.tanmay.shoppingapp.DataSet.DataProvider.insert(DataProvider.java:155)
                                                                                        at android.content.ContentProvider$Transport.insert(ContentProvider.java:264)
                                                                                        at android.content.ContentResolver.insert(ContentResolver.java:1279)
                                                                                        at com.example.tanmay.shoppingapp.YourCart.onCreate(YourCart.java:34)
                                                                                        at android.app.Activity.performCreate(Activity.java:6684)
                                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
                                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2652)
                                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2766)
                                                                                        at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1507)
                                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                        at android.os.Looper.loop(Looper.java:154)
                                                                                        at android.app.ActivityThread.main(ActivityThread.java:6236)
                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891)
                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)
    03-16 09:50:30.991 11672-11672/com.example.tanmay.shoppingapp E/com.whatever.tag: Failed to insert row for content://com.example.tanmay.shoppingapp/cart
    03-16 09:50:30.992 11672-11672/com.example.tanmay.shoppingapp E/SQLiteLog: (1) no such column: _id
    03-16 09:50:30.994 11672-11672/com.example.tanmay.shoppingapp D/AndroidRuntime: Shutting down VM
    03-16 09:50:30.996 11672-11672/com.example.tanmay.shoppingapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                    Process: com.example.tanmay.shoppingapp, PID: 11672
                                                                                    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tanmay.shoppingapp/com.example.tanmay.shoppingapp.YourCart}: android.database.sqlite.SQLiteException: no such column: _id (code 1): , while compiling: SELECT _id, quantity FROM cart
                                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2699)
                                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2766)
                                                                                        at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1507)
                                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                        at android.os.Looper.loop(Looper.java:154)
                                                                                        at android.app.ActivityThread.main(ActivityThread.java:6236)
                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891)
                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)
                                                                                     Caused by: android.database.sqlite.SQLiteException: no such column: _id (code 1): , while compiling: SELECT _id, quantity FROM cart
                                                                                        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                                        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
                                                                                        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
                                                                                        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                                        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                                                                                        at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
                                                                                        at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
                                                                                        at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1318)
                                                                                        at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1165)
                                                                                        at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1036)
                                                                                        at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1204)
                                                                                        at com.example.tanmay.shoppingapp.DataSet.DataProvider.query(DataProvider.java:92)
                                                                                        at android.content.ContentProvider.query(ContentProvider.java:1020)
                                                                                        at android.content.ContentProvider$Transport.query(ContentProvider.java:239)
                                                                                        at android.content.ContentResolver.query(ContentResolver.java:534)
                                                                                        at android.content.ContentResolver.query(ContentResolver.java:475)
                                                                                        at com.example.tanmay.shoppingapp.YourCart.onCreate(YourCart.java:44)
                                                                                        at android.app.Activity.performCreate(Activity.java:6684)
                                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
                                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2652)
                                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2766) 
                                                                                        at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1507) 
                                                                                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                        at android.os.Looper.loop(Looper.java:154) 
                                                                                        at android.app.ActivityThread.main(ActivityThread.java:6236) 
                                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891) 
                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781) 
    
    2 回复  |  直到 8 年前
        1
  •  0
  •   MikeT    8 年前

    由于某些原因,该表不包含名为 _id 。然而,这并不是因为SQL,即使SQL可能不会实现您的愿望。

    更具体地说,使用 INTEGER NON (而不是不) NULL 将不添加NOT NULL约束,而是为列提供column\u类型 整数非 因为它包含 内景 ,列类型(关联性)为 INTEGER

    例如(这使用 logDatabaseInfo to be found here )其中SQL为:-

    CREATE TABLE cart (_ID INTEGER NON NULL, quantity INTEGER)
    

    显示:-

    D/SQLITE_CSU: DatabaseList Row 1 Name=main File=/data/data/soupd.so49313202updatefailed/databases/mydb
    D/SQLITE_CSU: Database Version = 1
    D/SQLITE_CSU: Table Name = android_metadata Created Using = CREATE TABLE android_metadata (locale TEXT)
    D/SQLITE_CSU: Table = android_metadata ColumnName = locale ColumnType = TEXT Default Value = null PRIMARY KEY SEQUENCE = 0
    D/SQLITE_CSU: Table Name = cart Created Using = CREATE TABLE cart (_ID INTEGER NON NULL, quantity INTEGER)
    D/SQLITE_CSU: Table = cart ColumnName = _ID ColumnType = INTEGER NON Default Value = null PRIMARY KEY SEQUENCE = 0
    D/SQLITE_CSU: Table = cart ColumnName = quantity ColumnType = INTEGER Default Value = null PRIMARY KEY SEQUENCE = 0
    

    表=购物车ColumnName=\u ID ColumnType=整数非默认值=null 作为相关信息。

    SQL可能是:-

    CREATE TABLE cart (_ID INTEGER NOT NULL, quantity INTEGER)

    • 注意:通常\u id/\u id用于保存由SQLite自动生成的唯一标识符的列,因此 通常你会 _ID INTEGER PRIMARY KEY 对于列 释义对此进行编码,但不提供 _id号 将 导致SQLite生成唯一标识符(通常是 1、2、3、4……)。
    • 注意,我没有在下面的SQL中显示这一点,因为您需要查看不带id的插入。

    可能的真正问题

    真正的问题在其他地方,但很可能是由于DatabaseHelper的 onCreate 方法尚未调用。由于数据库存在,它可能被调用过一次。因此,最可能的问题是表结构已更改,但数据库尚未删除。

    更具体地说 创建时的回调函数 方法仅在创建数据库时(自动)调用一次。

    可能的解决方案

    可能的修复方法是删除数据库,然后重新运行应用程序。 -您可以通过删除应用程序的数据来删除数据库 -或者卸载应用程序。 -我建议按上面所示更改SQL。

        2
  •  0
  •   pastillas    8 年前

    尝试用以下语句替换create SQL\u create\u ENTERS语句,

     public static final String SQL_CREATE_ENTRIES =
        "CREATE TABLE " + TABLE_NAME + " ( " +
                _ID + " INTEGER NOT NULL, " +
                COLUMN_NAME_ORDERED_QUANTITY + " INTEGER)";
    

    您的查询在ID列中有无效的关键字“NON”。