Java 在android中将数据插入数据库时出错
我在插入项目详细信息时遇到如下错误:Java 在android中将数据插入数据库时出错,java,android,sqlite,syntax-error,android-sqlite,Java,Android,Sqlite,Syntax Error,Android Sqlite,我在插入项目详细信息时遇到如下错误: 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 (
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)`
com.example.android.inventoryapp E/SQLiteLog:(1)靠近“num”:语法错误
09-28 16:30:32.558 9471-9471/com.example.android.inventoryapp E/SQLiteDatabase:错误插入数量=1个名称=巧克力手机号码=9112价格=5个供应商名称=Venu
android.database.sqlite.SQLiteException:near“num”:语法错误(代码1),编译时:插入库存(数量、名称、电话号码、价格、供应商名称)值(?,,,,,?)
位于android.database.sqlite.SQLiteConnection.nativePrepareStatement(本机方法)
位于android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
位于android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
位于android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
位于android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:58)
位于android.database.sqlite.SQLiteStatement.(SQLiteStatement.java:31)
位于android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
位于android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
在com.example.android.inventoryapp.CatalogActivity.insertpet(CatalogActivity.java:104)上
在com.example.android.inventoryapp.CatalogActivity.onOptionsItemSelected上(CatalogActivity.java:118)
在android.app.Activity.onMenuItemSelected上(Activity.java:2885)
在android.support.v4.app.FragmentActivity.onMenuItemSelected上(FragmentActivity.java:407)
位于android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:195)`
列名中不能有空格。如果要使用空格,就像使用phone num
一样,应使用双引号(“
)来转义名称:
列名中不能有空格。如果要使用空格,就像使用
phone num
一样,应使用双引号(“
)对名称进行转义:
谢谢它起作用了……)我把我的数据库版本改成了两个,并且更改了列名…谢谢。它起作用了……)我将数据库版本更改为两个,并更改了列名。。。
INSERT INTO inventory("quantity", "name", "phone num", "price", "supp name")
VALUES (?, ?, ?, ?, ?)
Create a method and write the code to insert the data into table as below...
public boolean insertPetition1(String cap_date, String cap_time, String image_str, String latitude, String longitude,
String upload_status) {
SQLiteDatabase db = getWritableDatabase();
try {
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_IMAGE_PATH, image_str);
contentValues.put(COLUMN_CAPTURE_DATE, cap_date);
contentValues.put(COLUMN_CAPTURE_TIME, cap_time);
contentValues.put(COLUMN_LATITUDE, latitude);
contentValues.put(COLUMN_LONGITUDE, longitude);
contentValues.put(COLUMN_UPLOAD_STATUS, upload_status);
if (db.insert(MAIN_TABLE, null, contentValues) > 0) {
Log.e("Parameters", "Parameters:" + contentValues);
isInserted = true;
}
} catch (Exception e) {
Log.v("TAG", e.toString());
}
return isInserted;
}
Call this method where ever you want to insert data in the tabel ....
public void dbChanges() {
DatabaseHelper1 databaseMain = new
DatabaseHelper1(OfflineAttendanceChildWithOutAuto.this);
boolean status = false;
status = databaseMain.insertPetition1(date, currentTime, encodedImage, latitude, longitude, "", "", "", awccode, "", "", mainaddress, usercode, "", "", "1");
databaseMain.close();
}
It will work for sure try like this....