我正在尝试使用整数作为主键创建表,但它不起作用。 这是错误:
11-02 11:17:24.833:ERROR / AndroidRuntime(4781):引起: android.database.sqlite.SQLiteException:接近“0”:语法错误: CREATE TABLE tremp_db(0 INTEGER PRIMARY KEY AUTOINCREMENT,date TEXT,时间TEXT,重复TEXT,gamish TEXT,src_city TEXT,src_area TEXT,dest_city TEXT,dest_area TEXT,TEXT价格,TEXT,电话 TEXT,电子邮件TEXT,文本TEXT,update_time TEXT);
有人确切地知道这是什么问题吗?
...感谢名单
这是代码:
public class Data
{
private static String DATABASE_NAME = new String("db_carpool.db");
public static final int DATABASE_VERSION = 7;
public static String TABLE_NAME = new String("tremp_db");
public static int ID;
public static String DATE = new String("date");
private static String TIME = new String("time");
private static String REPEAT = new String("repeat");
private static String GAMISH = new String("gamish");
private static String SRC_CITY = new String("src_city");
public static String SRC_AREA = new String("src_area");
public static String DEST_CITY = new String("dest_city");
private static String DEST_AREA = new String("dest_area");
private static String PRICE = new String("price");
private static String WHO = new String("who");
public static String PHONE = new String("phone");
public static String EMAIL = new String("email");
private static String TEXT = new String("text");
private static String UPDATE_TIME = new String("update_time");
final Data thisActivity = this;
private DatabaseHelper mOpenHelper = null;
private static Data datab = null;
private class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null , DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL("CREATE TABLE " + TABLE_NAME + " ( "
+ ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ DATE + " TEXT, "
+ TIME + " TEXT, "
+ REPEAT + " TEXT, "
+ GAMISH + " TEXT, "
+ SRC_CITY + " TEXT, "
+ SRC_AREA + " TEXT, "
+ DEST_CITY + " TEXT, "
+ DEST_AREA + " TEXT, "
+ PRICE + " TEXT, "
+ WHO + " TEXT, "
+ PHONE + " TEXT, "
+ EMAIL + " TEXT, "
+ TEXT + " TEXT, "
+ UPDATE_TIME + " TEXT" + ");");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
,这是插入表
的函数的代码void NewTremp(Tremp tremp, Context context)
{
ContentValues values = new ContentValues();
//values.put(ID, tremp.id);
values.put(DATE, tremp.date);
values.put(TIME, tremp.time);
values.put(REPEAT, tremp.repeat);
values.put(GAMISH, tremp.gamish);
values.put(SRC_CITY, tremp.src_city);
values.put(SRC_AREA, tremp.src_area);
values.put(DEST_CITY, tremp.dest_city);
values.put(DEST_AREA, tremp.dest_area);
values.put(PRICE, tremp.price);
values.put(WHO, tremp.who);
values.put(PHONE, tremp.phone);
values.put(EMAIL, tremp.email);
values.put(TEXT, tremp.text);
values.put(UPDATE_TIME, tremp.update);
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
long rowId = db.insert(TABLE_NAME, null, values);
// handler.sendEmptyMessage(0);
}
答案 0 :(得分:5)
尝试将public static int ID;
的类型更改为String:
public static String ID = "_id";
如果要将db与CursorAdapter一起使用,则ID列需要命名为“_id”
修改强>
ID
类型的String
只是列的名称。列的类型在create table语句中定义:
db.execSQL("CREATE TABLE " + TABLE_NAME + " ( "
+ ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
因为您使用int
作为列名,所以您的列是以此整数的字符串表示形式命名的。
答案 1 :(得分:2)
您正在尝试将INTEGER值作为列名称,因此,SQLite不允许这样做。
Try to give any valid string column name for Integer ID type column like, as Herrmann suggested "_id" as a string.
答案 2 :(得分:1)
您的主键字段名称为0
。 SQL字段应以字母开头。 Here是字段名称的PostgreSQL规则(对于SQLite应该类似)。如果你真的想把字段名称保持为“0”,你可以尝试输入引号(“”)。