我是Android开发和SQLite数据库的新手,所以请关注我(也是我在这个网站上的第一个问题,希望它没关系)。
所以,我的数据库由三个表组成:
records
locations
tags
这就是我创建表格的方式:
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "recordsDatabase";
//Common columns
public static final String ID = "id";
public static final String NAME_COLUMN = "name";
//Tables
public static final String TABLE_RECORDS = "records";
public static final String TABLE_LOCATIONS = "locations";
public static final String TABLE_TAGS = "tags";
//Records table
public static final String KEY_CREATED_AT = "date";
public static final String STARTINGHOUR = "startingHour";
public static final String FINISHINGHOUR = "finishingHour";
public static final String COMMENTS = "comments";
public static final String LOCATIONS_ID = "locationsID";
public static final String TAGS_ID = "tagsID";
//Create table statements
public static final String CREATE_TABLE_RECORDS = "CREATE TABLE "
+ TABLE_RECORDS + "(" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ NAME_COLUMN + " TEXT,"
+ KEY_CREATED_AT + " DATETIME,"
+ STARTINGHOUR + " TEXT,"
+ FINISHINGHOUR + " TEXT,"
+ COMMENTS + " TEXT,"
+ LOCATIONS_ID + " INTEGER, "
+ TAGS_ID + " INTEGER, "
+ "FOREIGN KEY(" + LOCATIONS_ID + ") REFERENCES " + TABLE_LOCATIONS + "(id),"
+ "FOREIGN KEY(" + TAGS_ID + ") REFERENCES " + TABLE_TAGS + "(id))";
public static final String CREATE_TABLE_LOCATIONS = "CREATE TABLE "
+ TABLE_LOCATIONS + "(" + ID + " INTEGER PRIMARY KEY,"
+ NAME_COLUMN + ")";
public static final String CREATE_TABLE_TAGS = "CREATE TABLE "
+ TABLE_TAGS + "(" + ID + " INTEGER PRIMARY KEY,"
+ NAME_COLUMN + ")";
稍后,在onOpen
方法中,我创建了以下PRAGMA语句:
if(!db.isReadOnly()) db.execSQL("PRAGMA foreign_keys=ON;");
如您所见,我使用外键在这些表之间建立关系。正如我所提到的,我对SQLite数据库来说是全新的,所以这里也可能存在问题,但我没有收到任何相关的失败。
但是,我被告知以下错误:
AndroidRuntime: FATAL EXCEPTION: AsyncTask #1 ... java.lang.RuntimeException: An error occured while executing doInBackground()
...
Caused by: android.database.sqlite.SQLiteException: near ".": syntax error (code 1): , while compiling: SELECT records.id, records.name ,date, startingHour, finishingHour, comments, locationsID, tagsID locations.name, tags.name FROM records records, locations locations, tags tags WHERE records.locationsID = locations.id AND tagsID = tags.id
指向以下代码行:
public static final String RECORDS_ID_WITH_PREFIX = "rec.id";
public static final String RECORDS_NAME_WITH_PREFIX = "rec.name";
public static final String LOCATIONS_NAME_WITH_PREFIX = "loc.name";
public static final String TAGS_NAME_WITH_PREFIX = "tg.name";
private static final String WHERE_ID_EQUALS = DatabaseHelper.ID + " =?";
//...some blocks of code in between...\\
String query = "SELECT " + RECORDS_ID_WITH_PREFIX + ", "
+ RECORDS_NAME_WITH_PREFIX + " ,"
+ DatabaseHelper.KEY_CREATED_AT + ", "
+ DatabaseHelper.STARTINGHOUR + ", "
+ DatabaseHelper.FINISHINGHOUR + ", "
+ DatabaseHelper.COMMENTS + ", "
+ DatabaseHelper.LOCATIONS_ID + ", "
+ DatabaseHelper.TAGS_ID + " "+ LOCATIONS_NAME_WITH_PREFIX + ", " + TAGS_NAME_WITH_PREFIX + " FROM "
+ DatabaseHelper.TABLE_RECORDS + " rec, " + DatabaseHelper.TABLE_LOCATIONS + " loc, " + DatabaseHelper.TABLE_TAGS + " tg "
+ " WHERE rec." + DatabaseHelper.LOCATIONS_ID + " = loc." + DatabaseHelper.ID + " AND "
+ DatabaseHelper.TAGS_ID + " = tg." + DatabaseHelper.ID;
我之所以提出这个问题的原因是SELECT
查询应遵循以下语法:
SELECT [columns] FROM [table name] WHERE [conditions];
但如果我看一下我的语法,我认为没有错。现在我使用RECORDS_ID_WITH_PREFIX
,RECORDS_NAME_WITH_PREFIX
,LOCATIONS_NAME_WITH_PREFIX
和LOCATIONS_NAME_WITH_PREFIX
的原因是为了避免混淆,因为id
和name
列在所有表中都很常见。有人愿意看一看,帮助我理解我做错了吗?
另外,如果您认为我的问题有问题,请告诉我,以便将来不会再犯同样的错误。
答案 0 :(得分:2)
+ DatabaseHelper.TAGS_ID + " "+ LOCATIONS_NAME_WITH_PREFIX + ", " +
^^^
缺少逗号