アンドロイドスタジオでSQLiteデータベースを作成しようとしていますが、以下のエラーが表示されます。私はアンドロイドスタジオの初心者です。アンドロイドスタジオでデータベースを作成する際にエラーが発生しました
エラーは次のとおりです。
E/SQLiteLog: (1) near "_ItemName": syntax error
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: iq.nazaha.wissam.CoiApp, PID: 3786
java.lang.RuntimeException: Unable to start activity ComponentInfo{iq.nazaha.wissam.CoiApp/iq.nazaha.wissam.CoiApp.AddNewItemPage}: android.database.sqlite.SQLiteException: near "_ItemName": syntax error (code 1): , while compiling: CREATE TABLE items(_id INTEGER PRIMARY KEY _ItemName TEXT _ItemLocation TEXT _ItemPerson TEXT);
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: android.database.sqlite.SQLiteException: near "_ItemName": syntax error (code 1): , while compiling: CREATE TABLE items(_id INTEGER PRIMARY KEY _ItemName TEXT _ItemLocation TEXT _ItemPerson TEXT);
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.executeSql(SQLiteDatabase.java:1677)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1608)
at iq.nazaha.wissam.CoiApp.AppDB.onCreate(AppDB.java:32)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at iq.nazaha.wissam.CoiApp.AppDB.dbToString(AppDB.java:65)
at iq.nazaha.wissam.CoiApp.AddNewItemPage.printDB(AddNewItemPage.java:52)
at iq.nazaha.wissam.CoiApp.AddNewItemPage.onCreate(AddNewItemPage.java:32)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
そして、私のDBハンドラのコードは次のとおりです。
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.Cursor;
import android.content.Context;
import android.content.ContentValues;
public class AppDB extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "items.db";
private static final String TABLE_ITEMSINFO = "items";
public static final String COLUMN_ITEMID = "_id";
public static final String COLUMN_ITEMNAME = "_ItemName";
public static final String COLUMN_ITEMLOCATION = "_ItemLocation";
public static final String COLUMN_ITEMPERSON = "_ItemPerson";
public AppDB(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = " CREATE TABLE " + TABLE_ITEMSINFO + "(" +
COLUMN_ITEMID + " INTEGER PRIMARY KEY " +
COLUMN_ITEMNAME + " TEXT " +
COLUMN_ITEMLOCATION + " TEXT " +
COLUMN_ITEMPERSON + " TEXT " +
"); ";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(" DROP TABLE IF EXISTS" + TABLE_ITEMSINFO);
onCreate(db);
}
// Add to the database
public void AddItems(ItemsInfo itemsinfo){
ContentValues values = new ContentValues();
values.put(COLUMN_ITEMID, itemsinfo.get_id());
values.put(COLUMN_ITEMNAME, itemsinfo.get_ItemName());
values.put(COLUMN_ITEMLOCATION, itemsinfo.get_ItemLocation());
values.put(COLUMN_ITEMPERSON, itemsinfo.get_ItemPerson());
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_ITEMSINFO, null, values);
db.close();
}
// Delete from the database
public void DeleteItems(String ItemId){
SQLiteDatabase db = getWritableDatabase();
db.execSQL(" DELETE FROM " + TABLE_ITEMSINFO + " WHERE " + COLUMN_ITEMID + "=\"" + ItemId + "\";");
db.close();
}
// Print the database
public String dbToString(){
String dbString = "";
SQLiteDatabase db = getWritableDatabase();
String query = " SELECT * FROM " + TABLE_ITEMSINFO + " WHERE 1";
//CURSOR point to a location tn the database
Cursor c = db.rawQuery(query,null);
c.moveToFirst();
while (!c.isAfterLast()){
if (c.getString(c.getColumnIndex("itemname"))!= null){
dbString += c.getString(c.getColumnIndex("itemname"));
dbString += "\n";
}
}
db.close();
return dbString;
あなたの助けをありがとうございました。
はあなたがあなたのフィールド間**コンマ**を逃しているあなたのSQLクエリに
commas
を追加するのを忘れています定義。 –...誇張されています。 –