我想构建一个GPS数据库,其中每个表代表一次旅行。 为此,必须通过按下按钮创建一个新表。
到目前为止,我已经完成了所有工作。
但是我希望根据这种模式显示表格: Trip_1; Trip_2; Trip_3 ......
但是我总是得到表名为null的错误消息。
android.database.sqlite.SQLiteException: near "Table": syntax error (code 1): , while compiling: CREATE TABLE Trip_ (ID INTEGER PRIMARY KEY, Table TEXT)
请帮我找错。
这是我的数据库类:
public class GPSDatabase extends SQLiteOpenHelper {
String TAG = "GPSDatabase";
int tnumber = 0;
public static GPSDatabase INSTANCE = null;
private static final String DB_NAME = "eelo";
private static final int VERSION = 1;
public static String TABLE_NAME = "Trip_0";
public static final String TABLE_TRIP = "Trip_";
public static final String TABLE_COLUMN = "Table";
public static final String ID_COLUMN = "ID";
public static final String NUMBER_COLUMN = "Number";
public static final String DISTANCE_COLUMN = "Distance";
public static final String LATITUDE_COLUMN = "Latitude";
public static final String LONGITUDE_COLUMN = "Longitude";
private GPSDatabase(Context context) {
super(context, DB_NAME, null, VERSION);
}
public static GPSDatabase getInstance (final Context context){
if (INSTANCE == null){
INSTANCE = new GPSDatabase(context);
}
return INSTANCE;
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTripQuery =
"CREATE TABLE " + TABLE_TRIP + " (" + ID_COLUMN + " INTEGER PRIMARY KEY, " + TABLE_COLUMN + " TEXT)";
String createQuery;
if (tnumber==0) {
createQuery =
"CREATE TABLE " + TABLE_NAME + " (" + ID_COLUMN + " INTEGER PRIMARY KEY, " + NUMBER_COLUMN + " INTEGER DEFAULT NULL, " + DISTANCE_COLUMN + " INTEGER DEFAULT NULL, " + LATITUDE_COLUMN + " REAL DEFAULT NULL, " + LONGITUDE_COLUMN + " REAL DEFAULT NULL)";
}
else{
createQuery =
"CREATE TABLE " + getTrip(tnumber) + " (" + ID_COLUMN + " INTEGER PRIMARY KEY, " + NUMBER_COLUMN + " INTEGER DEFAULT NULL, " + DISTANCE_COLUMN + " INTEGER DEFAULT NULL, " + LATITUDE_COLUMN + " REAL DEFAULT NULL, " + LONGITUDE_COLUMN + " REAL DEFAULT NULL)";
}
db.execSQL(createTripQuery);
db.execSQL(createQuery);
Log.d(TAG, "Table created");
tnumber++;
}
在这里,我获得了新旅行的表名:
public String getTrip(final long id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_TRIP, new String[]{ID_COLUMN, TABLE_COLUMN}, ID_COLUMN + " = ?", new String[]{String.valueOf(id)}, null, null, null);
String table = null;
if (cursor != null && cursor.getCount() > 0) {
cursor.moveToFirst();
table = cursor.getString(cursor.getColumnIndex(TABLE_COLUMN));
}
Log.d(TAG, "getTable: "+table);
return table;
}
这是来自Main Activity的ONCLICK-Methode;
public static GPSDatabase db;
public static int tripNumber = 0;
public static boolean clicked=false;
int NAV = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
db = GPSDatabase.getInstance(this);
...
Button btn= (Button)findViewById(R.id.button3);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("new Trip ","clicked");
tripNumber++;
long newID = -1;
try{
SQLiteDatabase database = db.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(db.TABLE_COLUMN, "Trip_"+tripNumber);
newID = database.insert(db.TABLE_TRIP, null, values);
}catch (SQLiteException e){
Log.e(TAG,"insert");
}finally {
Log.d(TAG,"TableTrip insert with ID " + newID);
}
SQLiteDatabase db1;
db1 = db.getWritableDatabase();
db.TABLE_NAME = db.TABLE_TRIP+tripNumber;
String dropTable = "DROP TABLE IF EXISTS " + (db.TABLE_NAME+(tripNumber-1));
db1.execSQL(dropTable);
String createQuery =
"CREATE TABLE " + db.TABLE_NAME + " (" + db.ID_COLUMN + " INTEGER PRIMARY KEY, " + db.NUMBER_COLUMN + " INTEGER DEFAULT NULL, " + db.DISTANCE_COLUMN + " INTEGER DEFAULT NULL, " + db.LATITUDE_COLUMN + " REAL DEFAULT NULL, " + db.LONGITUDE_COLUMN + " REAL DEFAULT NULL)";
db1.execSQL(createQuery);
}
});
}
答案 0 :(得分:0)
问题在于将列命名为mainTabBar
中的表。 SQLite认为它遇到了关键字TABLE,这在语法上是错误的。
如果你真的想要一个名为Table的列,这是可能的,你可以使用public static final String TABLE_COLUMN = "Table";
。但是,这可能仍然存在问题并且令人困惑。
您也可以使用public static final String TABLE_COLUMN = "'Table'";
来避免使用推荐的(但相对很少使用)
的更多信息SQLite会不时添加新的关键字 特征。因此,为了防止您的代码被未来破坏 增强功能,您通常应引用任何标识符 英语单词,即使你没有。[强调补充]
所以也许_yourcolumnname
但是,也许更好的解决方案是应用规范化(减少数据库冗余)并使用一个列来标识 trip ,而不是具有多个结构相同的表。< / p>
e.g。你可以使用: -
public static final String TABLE_COLUMN = "_Table";
然后,您只需要一个表创建语句,而private static final String DB_NAME = "eelo";
private static final int VERSION = 1;
public static final String TABLE_TRIP = "Trip_";
public static final String ID_COLUMN = "ID";
public static final String NUMBER_COLUMN = "Number";
public static final String DISTANCE_COLUMN = "Distance";
public static final String LATITUDE_COLUMN = "Latitude";
public static final String LONGITUDE_COLUMN = "Longitude";
public static final String TRIP_COLUMN = "Trip";
方法中没有逻辑,例如它可能是: -
onCreate
然后你不需要@Override
public void onCreate(SQLiteDatabase db) {
String createQuery =
"CREATE TABLE " + TABLE_NAME + " (" +
ID_COLUMN + " INTEGER PRIMARY KEY, " +
NUMBER_COLUMN + " INTEGER DEFAULT NULL, " +
DISTANCE_COLUMN + " INTEGER DEFAULT NULL, " +
LATITUDE_COLUMN + " REAL DEFAULT NULL, " +
LONGITUDE_COLUMN + " REAL DEFAULT NULL +
TRIP_COLUMN + "INTEGER" +
)";
}
db.execSQL(createQuery);
Log.d(TAG, "Table created");
}
交换表,而只需要使用getTrip
(注意int可能很长,所以很可能有足够的数字)来实现相同的最终结果。