Android 创建时出现 SQLiteException
Android SQLiteException at create
首先我想说我是 android 的新人,如果这个问题很愚蠢,我深表歉意。
我正在为具有两个 table 的 SQLite 数据库编写内容提供程序。 table 是在导航抽屉中显示列表 activity,第二个 table 是在 ListFragment 中显示。每次启动应用程序时,我都会收到 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);
第一个table创建成功,第二个失败。
我已经搜索了几天以找到解决方案,但我失败了。那么有人可以帮我求情吗?
对不起我的英语。
这里是代码:
合约class第一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;
第二个合同classtable:
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;
数据库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)
和内容提供者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)
table
是一个 SQL 关键字,您不能将其用作列名。
将 KEY_PASS_TABLE
的值更改为其他值,例如"_table"
.
首先我想说我是 android 的新人,如果这个问题很愚蠢,我深表歉意。
我正在为具有两个 table 的 SQLite 数据库编写内容提供程序。 table 是在导航抽屉中显示列表 activity,第二个 table 是在 ListFragment 中显示。每次启动应用程序时,我都会收到 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);
第一个table创建成功,第二个失败。 我已经搜索了几天以找到解决方案,但我失败了。那么有人可以帮我求情吗?
对不起我的英语。
这里是代码:
合约class第一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;
第二个合同classtable:
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;
数据库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)
和内容提供者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)
table
是一个 SQL 关键字,您不能将其用作列名。
将 KEY_PASS_TABLE
的值更改为其他值,例如"_table"
.