gpt4 book ai didi

java - 在android中将数据插入数据库时​​出错

转载 作者:行者123 更新时间:2023-11-29 08:26:25 25 4
gpt4 key购买 nike

我在插入项目详细信息时遇到这样的错误:

09-28 16:30:32.558 9471-9471/com.example.android.inventoryapp E/SQLiteDatabase: Error inserting quantity=1 name=Chocolate phone num=9112 price=5 supp name=Venu
android.database.sqlite.SQLiteException: near "num": syntax error (code 1): , while compiling: INSERT INTO inventory(quantity,name,phone num,price,supp name) VALUES (?,?,?,?,?)

数据库助手是:

public void onCreate(SQLiteDatabase sqLiteDatabase) {
String SQL_CREATE_PRODUCTS_TABLE = "CREATE TABLE " + InventoryContract.InventoryEntry.TABLE_NAME + " ("
+ InventoryContract.InventoryEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ InventoryContract.InventoryEntry.COLUMN_Product_Name + " TEXT NOT NULL, "
+ InventoryContract.InventoryEntry.COLUMN_PRICE + " INTEGER NOT NULL, "
+ InventoryContract.InventoryEntry.COLUMN_Quantity + " INTEGER NOT NULL DEFAULT 0, "
+ InventoryContract.InventoryEntry.COLUMN_SUPPLIER_NAME + " TEXT NOT NULL, "
+ InventoryContract.InventoryEntry.COLUMN_SUPPLIER_Phno + " INTEGER NOT NULL);";
// Execute the SQL statement
//COLUMN_USERNAME + " TEXT "
sqLiteDatabase.execSQL(SQL_CREATE_PRODUCTS_TABLE);
}

合约类是:

 public final static class InventoryEntry implements BaseColumns{
public final static String TABLE_NAME="inventory";
public final static String _ID = BaseColumns._ID;
public final static String COLUMN_Product_Name="name";
public final static String COLUMN_PRICE="price";
public final static String COLUMN_Quantity="quantity";
public final static String COLUMN_SUPPLIER_NAME="supp name";
public final static String COLUMN_SUPPLIER_Phno="phone num";

}

logcat 响应是:

com.example.android.inventoryapp E/SQLiteLog: (1) near "num": syntax error
09-28 16:30:32.558 9471-9471/com.example.android.inventoryapp E/SQLiteDatabase: Error inserting quantity=1 name=Chocolate phone num=9112 price=5 supp name=Venu
android.database.sqlite.SQLiteException: near "num": syntax error (code 1): , while compiling: INSERT INTO inventory(quantity,name,phone num,price,supp name) 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:1469)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
at com.example.android.inventoryapp.CatalogActivity.insertpet(CatalogActivity.java:104)
at com.example.android.inventoryapp.CatalogActivity.onOptionsItemSelected(CatalogActivity.java:118)
at android.app.Activity.onMenuItemSelected(Activity.java:2885)
at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:407)
at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:195)`

最佳答案

列名中不能有空格。如果你想使用空格,就像你对 phone num 所做的那样,你应该用双引号将它们括起来 (") 来转义名称:

INSERT INTO inventory("quantity", "name", "phone num", "price", "supp name") 
VALUES (?, ?, ?, ?, ?)

关于java - 在android中将数据插入数据库时​​出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52554215/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com