gpt4 book ai didi

java - 编译时由: android. database.sqlite.SQLiteException : near "": syntax error (code 1): ,引起:

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:54:16 25 4
gpt4 key购买 nike

    11-27 03:32:04.471: E/AndroidRuntime(23137): Caused by: android.database.sqlite.SQLiteException: near "order": syntax error (code 1): , while compiling: create table order (_id integer primary key autoincrement, origin text not null, quantity integer not null); 

MyDatabase class:

public class MyDatabase extends SQLiteOpenHelper {

public static final String TABLE_NAME = "order";
public static final String TABLE_ID = "_id";
public static final String TABLE_ORIGIN = "origin";
public static final String TABLE_QUANTITY = "quantity";
private static final String DATABASE_NAME = "Appple.db";
private static final int DATABASE_VERSION = 1;

private static final String DATABASE_CREATE =
"create table " + TABLE_NAME +
"(" + TABLE_ID + " integer primary key autoincrement, " +
TABLE_ORIGIN + " text not null, " +
TABLE_QUANTITY + " integer not null);";

public MyDatabase (Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}

操作类:

public class Operation {

private MyDatabase dbHelper;
private String[] COLUMNS = { MyDatabase.TABLE_ID, MyDatabase.TABLE_ORIGIN, MyDatabase.TABLE_QUANTITY };
private SQLiteDatabase database;


public Operation(Context context) {
dbHelper = new MyDatabase(context);
}

public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}

public void close() {
dbHelper.close();
}

public void add(String origin, String quantity) {
ContentValues values = new ContentValues();
values.put(MyDatabase.TABLE_ORIGIN, origin);
values.put(MyDatabase.TABLE_QUANTITY, Integer.parseInt(quantity));
database.insert(MyDatabase.TABLE_NAME, null, values);
}

public int get(String origin) {
int total = 0;

Cursor cursor = database.query(MyDatabase.TABLE_NAME, COLUMNS,
MyDatabase.TABLE_ORIGIN + " = " + origin, null, null, null, null);
cursor.moveToFirst();
while(!cursor.isAfterLast()) {
total += cursor.getInt(2);
cursor.moveToNext();
}
cursor.close();
return total;
}
}

在 MainActivity 中开始于

Operation op;
op = new Operation(this);
op.open();

我觉得CREATE TABLE没有问题。但是我找不到错误的原因。

最佳答案

order 是 SQL 中的关键字。重命名表,或将表名放在双引号中,例如 "order"

关于java - 编译时由: android. database.sqlite.SQLiteException : near "": syntax error (code 1): ,引起:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27166793/

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