这就是我创建表格的方式
public class DataHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "find.db";
public static final int DATABASE_VERSION = 6;
public static final String TABELA_PERCURSOS = "percursos";
public static final String PERCURSO_ID = "_id";
public static final String CIDADE = "cidade";
public static final String CATEGORIA = "categoria";
public static final String INTERESSE = "interesse";
public static final String TABELA_CIDADES = "cidades";
public static final String ID_CIDADES = "_id";
public static final String NOME_CIDADES = "nome";
public static final String TABELA_CATEGORIAS = "categorias";
public static final String ID_CATEGORIAS = "_id";
public static final String NOME_CATEGORIAS = "nome_categorias";
public static final String TABELA_INTERESSES = "interesses";
public static final String ID_INTERESSES = "_id";
public static final String LATITUDE = "latitude";
public static final String LONGITUDE = "longitude";
public static final String DESCRICAO = "descricao";
public static final String NOME_INTERESSE = "nome_interesse";
public static final String ESTADO = "estado"; //0 se nao visitado; 1 se visitado
private static final String CREATE_TABELA_CIDADES = "create table " + TABELA_CIDADES + "( " + ID_CIDADES
+ " integer primary key autoincrement, " + NOME_CIDADES + " text not null unique )";
private static final String CREATE_TABELA_CATEGORIAS = "create table " + TABELA_CATEGORIAS + "( " + ID_CATEGORIAS
+ " integer primary key autoincrement, " + NOME_CATEGORIAS + " text not null unique )";
private static final String CREATE_TABELA_INTERESSES = "create table " + TABELA_INTERESSES + "( " + ID_INTERESSES
+ " integer primary key autoincrement, " + LATITUDE + " integer not null unique, " + LONGITUDE + " integer not null unique, "
+ DESCRICAO + " text not null unique, " + NOME_INTERESSE + " text not null unique, " + ESTADO + " integer)";
public static final String CREATE_TABELA_PERCURSOS = "create table " + TABELA_PERCURSOS + "( "
+ PERCURSO_ID + " integer primary key autoincrement, "
+ CIDADE + " integer not null, "
+ CATEGORIA + " integer not null, "
+ INTERESSE + " integer), " +
"foreign key (" + INTERESSE + ") references " + TABELA_INTERESSES + "(" + ID_INTERESSES + ")";
public DataHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABELA_CIDADES);
db.execSQL(CREATE_TABELA_CATEGORIAS);
db.execSQL(CREATE_TABELA_INTERESSES);
db.execSQL(CREATE_TABELA_PERCURSOS);
db.execSQL("PRAGMA foreign_keys = ON;");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABELA_CIDADES);
db.execSQL("DROP TABLE IF EXISTS " + TABELA_CATEGORIAS);
db.execSQL("DROP TABLE IF EXISTS " + TABELA_INTERESSES);
db.execSQL("DROP TABLE IF EXISTS " + TABELA_PERCURSOS);
onCreate(db);
}
这是我得到的错误:
**E/SQLiteLog: (1) near ",": syntax error**
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.nagisa.find, PID: 3627
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nagisa.find/com.example.nagisa.find.MainActivity}: **android.database.sqlite.SQLiteException: near ",": syntax error (code 1): , while compiling: create table percursos( _id integer primary key autoincrement, cidade integer not null, categoria integer not null, interesse integer), foreign key (interesse) references interesses(_id)
at **android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: android.database.sqlite.SQLiteException: near ",": syntax error (code 1): , while compiling: create table percursos( _id integer primary key autoincrement, cidade integer not null, categoria integer not null, interesse integer), foreign key (interesse) references interesses(_id)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1677)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1608)
at com.example.nagisa.find.DataHelper.onCreate(DataHelper.java:73)
at com.example.nagisa.find.DataHelper.onUpgrade(DataHelper.java:83)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:256)
at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
at com.example.nagisa.find.DataHelper.myCursor(DataHelper.java:143)
at com.example.nagisa.find.MainActivity.onCreate(MainActivity.java:22)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)