问题
first I want to say I'm new at android, so I'm apologizing if the question is to stupid.
I'm writting a Content Provider for a SQLite database with two tables. On table is to show a list at a navigation-drawer activity and the second table is to show in a ListFragment. Everytime when I'm starting the app I'm getting an SQLException.
10-07 15:30:33.856 28280-28813/de.schmidt.android.passworttresor E/SQLiteLog: (1) near "table": syntax error
10-07 15:30:33.871 28280-28813/de.schmidt.android.passworttresor E/AndroidRuntime: FATAL EXCEPTION: ModernAsyncTask #1
10-07 15:30:33.871 28280-28813/de.schmidt.android.passworttresor E/AndroidRuntime: Caused by: android.database.sqlite.SQLiteException: near "table": syntax error (code 1): , while compiling: create table passwords (_id integer primary key autoincrement, name text not null, user text, password text, url text, table text not null);
The first table will created successfully, but the second failed. I have searched for some days to find a solution, but i failed. So can anybody help me pleas?
Sorry for my english.
Here the code:
The contract class for the first table:
public static final String TABLE_LIST ="lists";
public static final String KEY_ID = "_id";
public static final String KEY_NAME = "name";
private static final String CREATE_LISTS_TABLE
= "create table if not exists "
+ TABLE_LIST
+ " ("
+ KEY_ID + " integer primary key autoincrement, "
+ KEY_NAME + " text not null "
+ ");";
public static final String LIST_TABLE_SCHEMA = CREATE_LISTS_TABLE;
The contract class for the second table:
public static final String TABLE_PASS = "passwords";
public static final String KEY_PASS_ID = "_id";
public static final String KEY_PASS_NAME = "name";
public static final String KEY_PASS_USER = "user";
public static final String KEY_PASS_PASS = "password";
public static final String KEY_PASS_URL = "url";
public static final String KEY_PASS_TABLE = "table";
private static final String CREATE_PASS_TABLE
= "create table "
+ TABLE_PASS
+ " ("
+ KEY_PASS_ID + " integer primary key autoincrement, "
+ KEY_PASS_NAME + " text not null, "
+ KEY_PASS_USER + " text, "
+ KEY_PASS_PASS + " text, "
+ KEY_PASS_URL + " text, "
+ KEY_PASS_TABLE + " text not null"
+ ");";
public static final String PASS_TABLE_SCHEMA = CREATE_PASS_TABLE;
Database class:
private static final String DEBUG_TAG = "Safe-SAFEDATABASE";
private static final String DB_NAME = "safe_data";
private static final int DB_VERSION = 1;
public SafeDataBase (Context context) {
super (context, DB_NAME, null, DB_VERSION);
} // public SafeDataBase (Context context)
@Override
public void onCreate (SQLiteDatabase db) {
db.execSQL(ListContract.LIST_TABLE_SCHEMA);
db.execSQL(PassContract.PASS_TABLE_SCHEMA);
} // public void onCreate (SQLiteDatabase db)
@Override
public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(DEBUG_TAG, "Upgrading " + ListContract.TABLE_LIST + " and " + PassContract.TABLE_PASS
+ " from " + oldVersion + " to " + newVersion + ". This deletes all dates!");
db.execSQL("DROP TABLE IF EXISTS " + ListContract.TABLE_LIST);
db.execSQL("DROP TABLE IF EXISTS " + PassContract.TABLE_PASS);
onCreate(db);
} // public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion)
and the content provider class:
>
private static final String AUTHORITY = "de.schmidt.android.safe.contentproviderclasses";
public static final int LISTS = 100;
public static final int PASSWORDS = 101;
public static final int LISTS_ID = 110;
public static final int PASSWORDS_ID = 111;
private static final String LISTS_BASE_PATH = "lists";
private static final String PASSWORDS_BASE_PATH = "passwords";
public static final Uri LISTS_URI = Uri.parse("content://" + AUTHORITY + "/" + LISTS_BASE_PATH);
public static final Uri PASSWORDS_URI
= Uri.parse("content://" + AUTHORITY + "/" + PASSWORDS_BASE_PATH);
private static final UriMatcher uriMatcher = new UriMatcher (UriMatcher.NO_MATCH);
static {
uriMatcher.addURI (AUTHORITY, LISTS_BASE_PATH, LISTS);
uriMatcher.addURI (AUTHORITY, PASSWORDS_BASE_PATH, PASSWORDS);
uriMatcher.addURI (AUTHORITY, LISTS_BASE_PATH + "/", LISTS_ID);
uriMatcher.addURI (AUTHORITY, LISTS_BASE_PATH + "/", PASSWORDS_ID);
}
private SafeDataBase dataBase;
//==============================================================================================
// Overridden Method's
//==============================================================================================
@Override
public boolean onCreate () {
dataBase = new SafeDataBase (getContext());
return true;
} // public boolean onCreate ()
@Nullable
@Override
public Cursor query (Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder qBuilder = new SQLiteQueryBuilder ();
int uriType = uriMatcher.match (uri);
switch (uriType) {
case LISTS_ID:
qBuilder.setTables (ListContract.TABLE_LIST);
qBuilder.appendWhere (ListContract.KEY_ID + "=" + uri.getLastPathSegment ());
break;
// case LISTS_ID:
case PASSWORDS_ID:
qBuilder.setTables (PassContract.TABLE_PASS);
qBuilder.appendWhere (PassContract.KEY_PASS_ID + "=" + uri.getLastPathSegment ());
break;
// case PASSWORDS_ID:
case LISTS:
break;
// case LISTS:
case PASSWORDS:
break;
// case PASSWORDS:
default:
throw new IllegalArgumentException ("Unknown URI");
// default:
} // switch (uriType)
Cursor cursor = qBuilder.query (dataBase.getReadableDatabase(), projection, selection,
selectionArgs, null, null, sortOrder);
cursor.setNotificationUri (getContext ().getContentResolver (), uri);
return cursor;
} // public Cursor query (Uri uri, String[] projection, String selection,
// String[] selectionArgs, String sortOrder)
@Nullable
@Override
public String getType (Uri uri) {
return null;
} // public String getType (Uri uri)
@Nullable
@Override
public Uri insert (Uri uri, ContentValues values) {
int uriType = uriMatcher.match (uri);
SQLiteDatabase db = dataBase.getWritableDatabase();
long newID = 0;
switch (uriType) {
case LISTS:
newID = db.insert (ListContract.TABLE_LIST, null, values);
if (newID > 0) {
Uri newUri = ContentUris.withAppendedId (uri, newID);
getContext ().getContentResolver ().notifyChange (uri, null);
return newUri;
} // if ( newID > 0)
else {
try {
throw new SQLException ("Failed to insert row into " + uri);
} // try
catch (SQLException e) {
e.printStackTrace();
} // catch (SQLException e)
} // else
break;
// case LISTS:
case PASSWORDS:
newID = db.insert (PassContract.TABLE_PASS, null, values);
if (newID > 0) {
Uri newUri = ContentUris.withAppendedId (uri, newID);
getContext ().getContentResolver ().notifyChange (uri, null);
return newUri;
} // if ( newID > 0)
else {
try {
throw new SQLException ("Failed to insert row into " + uri);
} // try
catch (SQLException e) {
e.printStackTrace();
} // catch (SQLException e)
} // else
break;
// case PASSWORDS:
default:
throw new IllegalArgumentException ("Invalid URI for insert");
// default:
} // switch (uriType)
return null;
} // public Uri insert (Uri uri, ContentValues values)
@Override
public int delete (Uri uri, String selection, String[] selectionArgs) {
int rowsAffected = 0;
int uriType = uriMatcher.match (uri);
SQLiteDatabase db = dataBase.getWritableDatabase ();
switch (uriType) {
case LISTS:
rowsAffected = db.delete (ListContract.TABLE_LIST, selection, selectionArgs);
break;
// case LISTS:
case LISTS_ID:
String id_list = uri.getLastPathSegment ();
if (TextUtils.isEmpty (selection)) {
rowsAffected = db.delete (ListContract.TABLE_LIST, ListContract.KEY_ID
+ "=" + id_list, null);
} // if (TextUtils.isEmpty (selection))
else {
rowsAffected = db.delete (ListContract.TABLE_LIST, selection + "and"
+ ListContract.KEY_ID + "=" + id_list, selectionArgs);
} // else
break;
//case LISTS_ID:
case PASSWORDS:
rowsAffected = db.delete (PassContract.TABLE_PASS, selection, selectionArgs);
break;
// case PASSWORDS:
case PASSWORDS_ID:
String id_pass = uri.getLastPathSegment ();
if (TextUtils.isEmpty (selection)) {
rowsAffected = db.delete (PassContract.TABLE_PASS, PassContract.KEY_PASS_ID
+ "=" + id_pass, null);
} // (TextUtils.isEmpty (selection))
else {
rowsAffected = db.delete (PassContract.TABLE_PASS, selection + "and"
+ PassContract.KEY_PASS_ID + "=" + id_pass, selectionArgs);
} // else
break;
// case PASSWORDS_ID:
default:
throw new IllegalArgumentException ("Unknown or invalid URI, " + uri);
} // switch (uriType)
getContext ().getContentResolver ().notifyChange (uri, null);
return rowsAffected;
} // public int delete (Uri uri, String selection, String[] selectionArgs)
@Override
public int update (Uri uri, ContentValues values,
String selection, String[] selectionArgs) {
SQLiteDatabase db = dataBase.getWritableDatabase ();
int rowsAffected = 0;
int uriType = uriMatcher.match (uri);
switch (uriType) {
case LISTS:
rowsAffected = db.update (ListContract.TABLE_LIST, values,
selection, selectionArgs);
break;
// case LISTS:
case LISTS_ID:
String id_list = uri.getLastPathSegment ();
StringBuilder modSelection_list = new StringBuilder (ListContract.KEY_ID
+ "=" + id_list);
if (!TextUtils.isEmpty(selection)) {
modSelection_list.append (" and " + selection);
} // if ( !TextUtils.isEmpty(selection))
rowsAffected = db.update (ListContract.TABLE_LIST, values,
modSelection_list.toString (), null);
break;
// case LISTS_ID:
case PASSWORDS:
rowsAffected = db.update (PassContract.TABLE_PASS, values,
selection, selectionArgs);
break;
// case PASSWORDS:
case PASSWORDS_ID:
String id_pass = uri.getLastPathSegment ();
StringBuilder modeSelection_pass = new StringBuilder (PassContract.KEY_PASS_ID
+ "=" + id_pass);
if (!TextUtils.isEmpty(selection)) {
modeSelection_pass.append (" and " + selection);
} // if (!TextUtils.isEmpty(selection))
rowsAffected = db.update (PassContract.TABLE_PASS, values,
modeSelection_pass.toString (), null);
break;
// case PASSWORDS_ID:
default:
throw new IllegalArgumentException ("Unknown URI");
// default:
} // switch (uriType)
getContext ().getContentResolver ().notifyChange (uri, null);
return rowsAffected;
} // public int update (Uri uri, ContentValues values,
// String selection, String[] selectionArgs)
回答1:
table
is an SQL keyword and you cannot use it as such as a column name.
Change the value of your KEY_PASS_TABLE
to something else, e.g. "_table"
.
来源:https://stackoverflow.com/questions/32994954/android-sqliteexception-at-create