在 sqlite 数据库中创建表时出现语法错误
Posted
技术标签:
【中文标题】在 sqlite 数据库中创建表时出现语法错误【英文标题】:syntax error in creat table in sqlite database 【发布时间】:2018-07-25 13:12:07 【问题描述】:我不知何故无法创建表。我检查了我的语法并搜索了其他有相同错误的人,但我找不到错误是什么。我使用相同的语法来创建所有表,但只是 stuff
表有问题。即使我将数据库版本更改为 2 和几次卸载我的应用程序。我在这里放了一部分代码。
public class Stuff
//*****DataBase query*****/
public static final String TABLE_NAME = "stuff";
public static final String CLM_ID = "id";
public static final String CLM_NAME = "name";
public static final String CLM_PRICE = "price";
public static final String CLM_DESCRIPTION = "description";
public static final String CLM_BRAND = "brand";
public static final String CLM_MODEL = "model";
public static final String CLM_COUNT = "count";
public static final String CLM_COLOR = "color";
public static final String CLM_CATGID = "catgid";
public static final String SQL_DROP_TABLE = String.format("DROP TABLE %s", TABLE_NAME);
public static final String SQL_CREATE_TABLE = String.format("CREATE TABLE IF NOT EXISTS %s (" +
"%s INTEGER PRIMARY KEY," +
"%s TEXT," +
"%s REAL," +
"%s TEXT NOT NULL," +
"%s TEXT NOT NULL," +
"%s TEXT," +
"%s INTEGER," +
"%s TEXT NOT NULL," +
"FOREIGN KEY (%s) REFERENCES(%s)" +
");", TABLE_NAME, CLM_ID, CLM_NAME,CLM_PRICE,CLM_DESCRIPTION,CLM_BRAND,CLM_MODEL,CLM_COUNT,CLM_COLOR,CLM_CATGID,Category.TABLE_NAME);
public class DBHelper extends SQLiteOpenHelper
private static DBHelper helper;
private static SQLiteDatabase database;
public static final String DATA_BASE_NAME = "dbDigikala";
public static final int DATA_BASE_VERSION = 1;
public static SQLiteDatabase getDatabase(Context context)
return getInstance(context).getWritableDatabase();
public static DBHelper getInstance(Context context)
if (helper == null)
helper = new DBHelper(context, DATA_BASE_NAME, null, DATA_BASE_VERSION);
return helper;
public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version)
super(context, name, factory, version);
@Override
public void onCreate(SQLiteDatabase db)
db.execSQL(Category.SQL_CREATE_TABLE);
db.execSQL(Stuff.SQL_CREATE_TABLE);
db.execSQL(Rate.SQL_CREATE_TABLE);
db.execSQL(Favorit.SQL_CREATE_TABLE);
db.execSQL(Image.SQL_CREATE_TABLE);
db.execSQL(Video.SQL_CREATE_TABLE);
db.execSQL(Address.SQL_CREATE_TABLE);
db.execSQL(User.SQL_CREATE_TABLE);
db.execSQL(Comment.SQL_CREATE_TABLE);
db.execSQL(Factor.SQL_CREATE_TABLE);
db.execSQL(SoldStuff.SQL_CREATE_TABLE);
db.execSQL(Slake.SQL_CREATE_TABLE);
db.execSQL(SlakeStuff.SQL_CREATE_TABLE);
db.execSQL(SlakeUser.SQL_CREATE_TABLE);
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
db.execSQL(Category.SQL_DROP_TABLE);
db.execSQL(Stuff.SQL_DROP_TABLE);
db.execSQL(Rate.SQL_DROP_TABLE);
db.execSQL(Favorit.SQL_DROP_TABLE);
db.execSQL(Image.SQL_DROP_TABLE);
db.execSQL(Video.SQL_DROP_TABLE);
db.execSQL(Address.SQL_DROP_TABLE);
db.execSQL(User.SQL_DROP_TABLE);
db.execSQL(Comment.SQL_DROP_TABLE);
db.execSQL(Factor.SQL_DROP_TABLE);
db.execSQL(SoldStuff.SQL_DROP_TABLE);
db.execSQL(Slake.SQL_DROP_TABLE);
db.execSQL(SlakeStuff.SQL_DROP_TABLE);
db.execSQL(SlakeUser.SQL_DROP_TABLE);
onCreate(db);
public class Config extends Application
public static Context context;
public static Activity currentActivity;
public static final long SPLASH_DELAY = 2000;
public static SQLiteDatabase database;
@Override
public void onCreate()
super.onCreate();
context = getApplicationContext();
database = DBHelper.getDatabase(getApplicationContext());
【问题讨论】:
这会是问题吗?看起来外键还没有定义? You have to define your column first and then set foreign key on it. 请不要发布代码、XML 或 logcat 输出的屏幕截图。请将所有文本作为文本发布。 【参考方案1】:问题在于您设置 ForeignKey "FOREIGN KEY (%s) REFERENCES(%s)"
。
更新您的代码如下:
public static final String SQL_CREATE_TABLE = String.format("CREATE TABLE IF NOT EXISTS %s (" +
"%s INTEGER PRIMARY KEY," +
"%s TEXT," +
"%s REAL," +
"%s TEXT NOT NULL," +
"%s TEXT NOT NULL," +
"%s TEXT," +
"%s INTEGER," +
"%s TEXT NOT NULL," +
"FOREIGN KEY (%s) REFERENCES %s(%s)" +
");", TABLE_NAME, CLM_ID, CLM_NAME,CLM_PRICE,CLM_DESCRIPTION,CLM_BRAND,CLM_MODEL,CLM_COUNT,CLM_COLOR,CLM_CATGID,Category.TABLE_NAME,Category.CLM_ID);
您仅传递表名并将其设置在错误的位置。
"FOREIGN KEY (%s) REFERENCES %s(%s)"
REFERENCES 之后的第一个 %s
是表名,第二个 (%s)
是您在类别表中的列 id。
【讨论】:
我这样做了,但我的应用程序给了我同样的错误。我认为问题来自其他地方。例如dBHelper
类。
类别表中的 id(主键)是什么?您最后必须传递该列名而不是 Category.CLM_ID
。并尝试清除数据并安装一次最新版本。
Category 表的主键是 That's what you write (Category.CLM_ID
)。我也清除数据。
试试这个"FOREIGN KEY (%s) REFERENCES %s(%s)"
删除%s后的空格
很高兴为您提供帮助!!【参考方案2】:
子句REFERENCES
必须使用语法:REFERENCES table_name(column_id)
:
public static final String SQL_CREATE_TABLE = String.format("CREATE TABLE IF NOT EXISTS %s (" +
"%s INTEGER PRIMARY KEY," +
"%s TEXT," +
"%s REAL," +
"%s TEXT NOT NULL," +
"%s TEXT NOT NULL," +
"%s TEXT," +
"%s INTEGER," +
"%s TEXT NOT NULL," +
"FOREIGN KEY (%s) REFERENCES %s(%s)" +
");", TABLE_NAME, CLM_ID, CLM_NAME,CLM_PRICE,CLM_DESCRIPTION,CLM_BRAND,CLM_MODEL,CLM_COUNT,CLM_COLOR,CLM_CATGID,Category.TABLE_NAME,Category.CLM_ID);
【讨论】:
以上是关于在 sqlite 数据库中创建表时出现语法错误的主要内容,如果未能解决你的问题,请参考以下文章
在 SQL Server Management Studio 中创建存储过程时出现语法错误
尝试在 H2 数据库中创建表时出现 Liquibase 错误
尝试将 json 导入 mongodb 云集合时出现语法错误
android.database.sqlite.SQLiteException:在“Foreign”附近:语法错误(代码 1):