我在创建表格时遇到了这个错误,但我找不到我的错误产生的位置。
06-11 23:22:59.060:E / SQLiteLog(1100):( 1)接近“INTEGER”:语法 错误06-11 23:22:59.110:E / AndroidRuntime(1100):致命异常: main 06-11 23:22:59.110:E / AndroidRuntime(1100): java.lang.RuntimeException:无法启动活动 ComponentInfo {com.khalil.dabaresto / com.khalil.dabaresto.MainActivity}: android.database.sqlite.SQLiteException:接近“INTEGER”:语法错误 (代码1):,编译时:CREATE TABLE plat(idplat INTEGER PRIMARY KEY,platname TEXT,prixplat INTEGER,refcategorie INTEGER NOT NULL, FOREIGN KEY(refcategorie)参考类别 (idcategorierefingredient INTEGER NOT NULL,FOREIGN KEY(refingredient)参考成分(redredient));
这是我的数据库:
package com.khalil.dabaresto;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.Cursor;
import android.widget.Spinner;
import android.content.Context;
import android.content.ContentValues;
public class MyDBHandler extends SQLiteOpenHelper{
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "resto.db";
private static final String TABLE_RESTAURANT = "restaurant";
private static final String COLUMN_ID = "id";
private static final String COLUMN_RESTAURANTNAME = "restaurantname";
private static final String COLUMN_REFVILLE = "refville";
private static final String TABLE_MENU ="menu";
private static final String COLUMN_IDMENU ="idmenu";
private static final String COLUMN_MENUNAME ="menuname";
private static final String COLUMN_PRIXMENU = "prixmenu";
private static final String COLUMN_REFRESTAURANT ="refrestaurant";
private static final String TABLE_PLAT ="plat";
private static final String COLUMN_IDPLAT ="idplat";
private static final String COLUMN_PLATNAME ="platname";
private static final String COLUMN_PRIXPLAT ="prixplat";
private static final String COLUMN_REFCATEGORIE ="refcategorie";
private static final String COLUMN_REFINGREDIENT ="refingredient";
private static final String TABLE_INGREDIENT ="ingredient";
private static final String COLUMN_IDINGREDIENT ="idingredient";
private static final String COLUMN_INGREDIENTNAME ="ingredientname";
private static final String TABLE_CATEGORIE ="categorie";
private static final String COLUMN_IDCATEGORIE ="idcategorie";
private static final String COLUMN_CATEGORIENAME ="categoriename";
private static final String TABLE_VILLE ="ville";
private static final String COLUMN_IDVILLE ="idville";
private static final String COLUMN_VILLENAME ="villename";
public MyDBHandler(Context context, String name, CursorFactory factory,
int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query ="CREATE TABLE " + TABLE_RESTAURANT + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY, " +
COLUMN_RESTAURANTNAME + " TEXT " +
")";
db.execSQL(query);
String query1 = "CREATE TABLE " + TABLE_MENU + "(" +
COLUMN_IDMENU + " INTEGER PRIMARY KEY, " +
COLUMN_MENUNAME + " TEXT, " +
COLUMN_PRIXMENU + " INTEGER, " +
COLUMN_REFRESTAURANT + " INTEGER NOT NULL, FOREIGN KEY ("+COLUMN_REFRESTAURANT+") REFERENCES "+TABLE_RESTAURANT+" ( "+COLUMN_ID+") );";
db.execSQL(query1);
String query2 = " CREATE TABLE " + TABLE_PLAT + "(" +
COLUMN_IDPLAT + " INTEGER PRIMARY KEY, " +
COLUMN_PLATNAME + " TEXT, " +
COLUMN_PRIXPLAT + " INTEGER, " +
COLUMN_REFCATEGORIE + " INTEGER NOT NULL, " +" FOREIGN KEY ("+COLUMN_REFCATEGORIE+") REFERENCES " +TABLE_CATEGORIE+" ("+COLUMN_IDCATEGORIE+
COLUMN_REFINGREDIENT + " INTEGER NOT NULL, " +" FOREIGN KEY("+COLUMN_REFINGREDIENT+ ") REFERENCES " +TABLE_INGREDIENT+" ( "+COLUMN_IDINGREDIENT+") );";
db.execSQL(query2);
String query3 ="CREATE TABLE" + TABLE_INGREDIENT + "(" +
COLUMN_IDINGREDIENT + " INTEGER PRIMARY KEY, " +
COLUMN_INGREDIENTNAME +" TEXT" +
");";
db.execSQL(query3);
String query4 = "CREATE TABLE" + TABLE_CATEGORIE + "(" +
COLUMN_IDCATEGORIE + " INTEGER PRIMARY KEY, " +
COLUMN_CATEGORIENAME +" TEXT" +
");";
db.execSQL(query4);
String query5 = "CREATE TABLE" + TABLE_VILLE + "(" +
COLUMN_IDVILLE + " INTEGER PRIMARY KEY, " +
COLUMN_VILLENAME +" TEXT" +
");";
db.execSQL(query5);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_RESTAURANT);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_MENU);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_PLAT);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_INGREDIENT);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CATEGORIE);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_VILLE);
onCreate(db);
}
public void addRestaurant(Restaurant restaurant,Ville ville){
ContentValues values = new ContentValues();
values.put(COLUMN_RESTAURANTNAME, restaurant.get_restaurantname());
values.put(COLUMN_REFVILLE,ville.get_idville() );
SQLiteDatabase db= getWritableDatabase();
db.insert(TABLE_RESTAURANT, null, values);
db.close();
}
public void addMenu(Menu menu,Restaurant restaurant){
ContentValues values = new ContentValues();
values.put(COLUMN_MENUNAME, menu.get_menuname());
values.put(COLUMN_PRIXMENU,menu.get_prixmenu() );
values.put(COLUMN_REFRESTAURANT, restaurant.get_id());
SQLiteDatabase db= getWritableDatabase();
db.insert(TABLE_MENU, null, values);
db.close();
}
public void addPlat(Plat plat,Categorie categorie,Ingredient ingredient){
ContentValues values = new ContentValues();
values.put(COLUMN_PLATNAME, plat.get_platname());
values.put(COLUMN_PRIXPLAT,plat.get_prixplat() );
values.put(COLUMN_REFCATEGORIE, categorie.get_idcategorie());
values.put(COLUMN_REFINGREDIENT, ingredient.get_idingredient());
SQLiteDatabase db= getWritableDatabase();
db.insert(TABLE_PLAT, null, values);
db.close();
}
public void addIngredient(Ingredient ingredient){
ContentValues values = new ContentValues();
values.put(COLUMN_INGREDIENTNAME, ingredient.get_ingredientname());
SQLiteDatabase db= getWritableDatabase();
db.insert(TABLE_INGREDIENT, null, values);
db.close();
}
public void addCategorie(Categorie categorie){
ContentValues values = new ContentValues();
values.put(COLUMN_CATEGORIENAME, categorie.get_categoriename());
SQLiteDatabase db= getWritableDatabase();
db.insert(TABLE_CATEGORIE, null, values);
db.close();
}
public void addVille(Ville ville){
ContentValues values = new ContentValues();
values.put(COLUMN_VILLENAME, ville.get_villename());
SQLiteDatabase db= getWritableDatabase();
db.insert(TABLE_VILLE, null, values);
db.close();
}
public void deleteRestaurant(String restaurantName){
SQLiteDatabase db=getWritableDatabase();
db.execSQL("DELETE FROM " + TABLE_RESTAURANT + " WHERE " + COLUMN_RESTAURANTNAME + "=\"" + restaurantName + "\";" );
}
public void deletePlat(String platName){
SQLiteDatabase db=getWritableDatabase();
db.execSQL("DELETE FROM " +TABLE_PLAT + "WHERE " + COLUMN_PLATNAME+ "=\""+platName+"\";");
}
public String databaseToString(){
String dbString = "";
SQLiteDatabase db =getWritableDatabase();
String query ="SELECT * FROM"+TABLE_RESTAURANT + "WHERE 1";
Cursor c=db.rawQuery(query, null);
c.moveToFirst();
while (!c.isAfterLast()){
if(c.getString(c.getColumnIndex(" restaurantname"))!=null){
dbString += c.getString(c.getColumnIndex(" restaurantname"));
dbString +="\n";
}
}
db.close();
return dbString;
}
public String databaseToString1(){
String dbString1 = "";
SQLiteDatabase db1 =getWritableDatabase();
String query1 ="SELECT * FROM"+TABLE_MENU + "WHERE 1";
Cursor c1=db1.rawQuery(query1, null);
c1.moveToFirst();
while (!c1.isAfterLast()){
if(c1.getString(c1.getColumnIndex("menuname"))!=null){
dbString1 += c1.getString(c1.getColumnIndex("menuname"));
dbString1 +="\n";
}
}
db1.close();
return dbString1;
}
public String databaseToString2(){
String dbString2 = "";
SQLiteDatabase db2 =getWritableDatabase();
String query2 ="SELECT * FROM"+TABLE_PLAT + "WHERE 1";
Cursor c2=db2.rawQuery(query2, null);
c2.moveToFirst();
while (!c2.isAfterLast()){
if(c2.getString(c2.getColumnIndex("platname"))!=null){
dbString2 += c2.getString(c2.getColumnIndex("platname"));
dbString2 +="\n";
}
}
db2.close();
return dbString2;
}
public String databaseToString3(){
String dbString3 = "";
SQLiteDatabase db3 =getWritableDatabase();
String query3 ="SELECT * FROM"+TABLE_INGREDIENT + "WHERE 1";
Cursor c3=db3.rawQuery(query3, null);
c3.moveToFirst();
while (!c3.isAfterLast()){
if(c3.getString(c3.getColumnIndex("ingredientname"))!=null){
dbString3 += c3.getString(c3.getColumnIndex("ingredientname"));
dbString3 +="\n";
}
}
db3.close();
return dbString3;
}
public String databaseToString4(){
String dbString4 = "";
SQLiteDatabase db4 =getWritableDatabase();
String query4 ="SELECT * FROM"+TABLE_CATEGORIE + "WHERE 1";
Cursor c4=db4.rawQuery(query4, null);
c4.moveToFirst();
while (!c4.isAfterLast()){
if(c4.getString(c4.getColumnIndex("categoriename"))!=null){
dbString4 += c4.getString(c4.getColumnIndex("categoriename"));
dbString4 +="\n";
}
}
db4.close();
return dbString4;
}
public String databaseToString5(){
String dbString5 = "";
SQLiteDatabase db5 =getWritableDatabase();
String query5 ="SELECT * FROM"+TABLE_VILLE + "WHERE 1";
Cursor c5=db5.rawQuery(query5, null);
c5.moveToFirst();
while (!c5.isAfterLast()){
if(c5.getString(c5.getColumnIndex("villename"))!=null){
dbString5 += c5.getString(c5.getColumnIndex("villename"));
dbString5 +="\n";
}
}
db5.close();
return dbString5;
}
}
答案 0 :(得分:1)
最后一行(query2)中缺少“)”:
COLUMN_REFCATEGORIE + " INTEGER NOT NULL, " +" FOREIGN KEY ("+COLUMN_REFCATEGORIE+") REFERENCES " +TABLE_CATEGORIE+" ("+COLUMN_IDCATEGORIE+ ")" +