sqlite3在android中创建表错误(sqlite3 create table error in android)
我试图在android中使用sqlite3创建表我的开放助手是这样的:
public class TaskOpenHelper extends BaseOpenHelper {
public static final String TABLE_NAME = "tasks";
public static final String COL_ID = "id";
public static final String COL_USER_ID = "user_id";
public static final String COL_TITLE = "title";
public static final String COL_DATE = "date";
public static final String COL_TIME = "time";
public static final String COL_IS_DONE = "is_done";
public static final String COL_IS_IPM = "is_ipm";
public static final String COL_DESC = "desc";
public static final String COL_REGISTER_TIME = "register_time";
public static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME +
"(" +
COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
COL_USER_ID + " INTEGER NOT NULL," +
COL_TITLE + " TEXT NOT NULL," +
COL_DATE + " TEXT ," +
COL_TIME + " TEXT ," +
COL_IS_DONE + " INTEGER ," +
COL_IS_IPM + " INTEGER ," +
COL_DESC + " TEXT," +
COL_REGISTER_TIME + " TEXT," +
"FOREIGN KEY " + COL_USER_ID + " REFERENCES " + UserOpenHelper.TABLE_NAME + "(" + UserOpenHelper.COL_ID + ")" +
");";
public TaskOpenHelper(Context context) {
super(context, DATABASE_NAME, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(sqLiteDatabase);
}
但是当我在尝试创建表时运行app时,这个错误得到了证实:
Caused by: android.database.sqlite.SQLiteException: near "user_id": syntax error (code 1): , while compiling: CREATE TABLE IF NOT EXISTS tasks(id INTEGER PRIMARY KEY AUTOINCREMENT,user_id INTEGER NOT NULL,title TEXT NOT NULL,date TEXT ,time TEXT ,is_done INTEGER ,is_ipm INTEGER ,desc TEXT,register_time TEXT,FOREIGN KEY user_id REFERENCES user(Id));
这个open helper在数据源类中用于执行db的查询
I am trying to create table in android using sqlite3 my open helper is this:
public class TaskOpenHelper extends BaseOpenHelper {
public static final String TABLE_NAME = "tasks";
public static final String COL_ID = "id";
public static final String COL_USER_ID = "user_id";
public static final String COL_TITLE = "title";
public static final String COL_DATE = "date";
public static final String COL_TIME = "time";
public static final String COL_IS_DONE = "is_done";
public static final String COL_IS_IPM = "is_ipm";
public static final String COL_DESC = "desc";
public static final String COL_REGISTER_TIME = "register_time";
public static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME +
"(" +
COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
COL_USER_ID + " INTEGER NOT NULL," +
COL_TITLE + " TEXT NOT NULL," +
COL_DATE + " TEXT ," +
COL_TIME + " TEXT ," +
COL_IS_DONE + " INTEGER ," +
COL_IS_IPM + " INTEGER ," +
COL_DESC + " TEXT," +
COL_REGISTER_TIME + " TEXT," +
"FOREIGN KEY " + COL_USER_ID + " REFERENCES " + UserOpenHelper.TABLE_NAME + "(" + UserOpenHelper.COL_ID + ")" +
");";
public TaskOpenHelper(Context context) {
super(context, DATABASE_NAME, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(sqLiteDatabase);
}
but when I run app when trying to create table this error accured:
Caused by: android.database.sqlite.SQLiteException: near "user_id": syntax error (code 1): , while compiling: CREATE TABLE IF NOT EXISTS tasks(id INTEGER PRIMARY KEY AUTOINCREMENT,user_id INTEGER NOT NULL,title TEXT NOT NULL,date TEXT ,time TEXT ,is_done INTEGER ,is_ipm INTEGER ,desc TEXT,register_time TEXT,FOREIGN KEY user_id REFERENCES user(Id));
this open helper is used in a datasource class to executre querys from db
原文:https://stackoverflow.com/questions/32017910
更新时间:2019-06-16 07:52
最满意答案
您需要将user_id放在FOREIGN KEY子句的括号中:
FOREIGN KEY (user_id) REFERENCES user(Id)
整个陈述应该是这样的
CREATE TABLE IF NOT EXISTS tasks(id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
title TEXT NOT NULL,
date TEXT,
time TEXT,
is_done INTEGER,
is_ipm INTEGER,
desc TEXT,
register_time TEXT,
FOREIGN KEY user_id REFERENCES user(Id));
此外,您必须更改desc列的名称,因为这是SQLite中的保留字 。
You need to put user_id in parentheses in the FOREIGN KEY clause:
FOREIGN KEY (user_id) REFERENCES user(Id)
The entire statement should look like
CREATE TABLE IF NOT EXISTS tasks(id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
title TEXT NOT NULL,
date TEXT,
time TEXT,
is_done INTEGER,
is_ipm INTEGER,
desc TEXT,
register_time TEXT,
FOREIGN KEY user_id REFERENCES user(Id));
Also you must change the name of the desc column as this is a reserved word in SQLite.
2015-08-14
相关问答
您需要将user_id放在FOREIGN KEY子句的括号中: FOREIGN KEY (user_id) REFERENCES user(Id)
整个陈述应该是这样的 CREATE TABLE IF NOT EXISTS tasks(id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
title TEXT NOT NULL,
date TEXT,
time TEXT,
is_do
...
尝试这个: create table tbl1(id int primary key, dt datetime default current_timestamp);
背景: DEFAULT约束指定在执行INSERT时使用的默认值。 该值可能为NULL,字符串常量,数字或括在括号中的常量表达式。 默认值也可以是特殊情况独立关键字CURRENT_TIME,CURRENT_DATE或CURRENT_TIMESTAMP之一。 如果值为NULL,则字符串常量或数字,只要不指定列的值的INSERT语句被执
...
通常当您遇到此类错误时,这是因为您使用关键字作为列(或表)名称。 我看到你有一个名为primary的列。 您需要在其周围添加反引号或重命名它,因为它是SQLite中的关键字 ; 例如: ...
`Primary` TEXT,
...
Often when you get this kind of error it is because you are using keywords as column (or table) names. I see that you have a column c
...
我没有给你一个明确的答案,因为sqlite可能会出现很多问题。 我能看到的主要是你没有初始化databasePath 。 这需要是.db文件的路径,例如〜/ Desktop / myDatabase.db。 1)我假设你只想处理一次statement而你实际上是插入它3次。 你需要检查rc的值,即只调用sqlite3_step一次,然后将rc放入if语句: int rc =sqlite3_step(statement);
if(rc == SQLITE_DONE){
...
您需要检查应用程序文档目录中的“users.db”文件。 如果您正在使用模拟器,您将获得以下行中的路径 let dirPaths =
NSSearchPathForDirectoriesInDomains(.DocumentDirectory,
.UserDomainMask, true)
复制路径并右键单击finder。 单击“转到文件夹...”选项。 粘贴路径并单击“开始” 要么 如果您使用iPhone之类的设备,那么在iFunbox( 这里 )的帮
...
只需确保将用于创建数据库的相同字符串名称传递给getDatbasePath()方法。 更换 File database = getApplicationContext().getDatabasePath("GeoLocator.db");
同 File database = getApplicationContext().getDatabasePath("GeoLocator");
Just make sure that you pass in the same string name you
...
Transaction是一个保留关键字 ,您不能将其用作标识符。 只需使用Transaction以外的其他内容作为标识符。 Transaction is a reserved keyword which you are not allowed to use as an identifier. Just use something other than Transaction as an identifier.
使用destroy scaffold不会运行回滚到迁移。 这样做的正确方法是 rake db:rollback rails destroy scaffold level 现在,由于您没有其他迁移,您无法回滚。 您需要手动删除该表: rails dbconsole DROP TABLE levels; 这应该可以解决您的问题 Using destroy scaffold does not run the rollback to the migration. The correct way of do
...
您可以尝试使用SQLiteAssetHelper ,因为它旨在使开发人员可以轻松地使用他们的应用程序打包数据库。 话虽这么说,你的数据库太大了。 有很多1.6设备无法为你的APK和解压缩的数据库提供空间。 You can attempt to use SQLiteAssetHelper for this, as it is designed to make it easy for developers to package a database with their app. That being
...
好的,我解决了这个问题,并认为我应该发布解决方案以供将来参考。 基本上所有需要做的就是添加 foreign_key: "learnable_id" 至 "has_many :coursejoins, dependent: :destroy" 在课程模型和 foreign_key: "student_id" 至 has_many :coursejoins, dependent: :destroy 到用户模型。 Okay i got this solved and thought i should p
...