【问题标题】:Android SQLiteException at createAndroid SQLiteException 在创建
【发布时间】:2016-01-04 20:17:53
【问题描述】:

首先我想说我是 android 的新手,所以如果这个问题很愚蠢,我很抱歉。

我正在为带有两个表的 SQLite 数据库编写内容提供程序。上表是在导航抽屉活动中显示列表,第二个表是在 ListFragment 中显示。每次启动应用程序时,我都会收到 SQLException。

10-07 15:30:33.856 28280-28813/de.schmidt.android.passworttresor E/SQLiteLog:(1)“表”附近:语法错误

10-07 15:30:33.871 28280-28813/de.schmidt.android.passworttresor E/AndroidRuntime:致命异常:ModernAsyncTask #1

10-07 15:30:33.871 28280-28813/de.schmidt.android.passworttresor E/AndroidRuntime:由:android.database.sqlite.SQLiteException:靠近“表”:语法错误(代码 1):,编译时:创建表密码(_id整数主键自增,名称文本不为空,用户文本,密码文本,url文本,表文本不为空);

第一个表将创建成功,但第二个失败。 我已经搜索了几天以找到解决方案,但我失败了。那么有谁能帮帮我吗?

对不起我的英语。

代码如下:

第一个表的合约类:

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;

第二个表的合约类:

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;

数据库类:

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)

和内容提供者类:

>

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)

【问题讨论】:

    标签: android sqlite android-contentprovider multiple-tables


    【解决方案1】:

    table 是 SQL 关键字,不能将其用作列名。

    KEY_PASS_TABLE 的值更改为其他值,例如"_table".

    【讨论】:

      猜你喜欢
      • 2017-08-24
      • 1970-01-01
      • 2013-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-30
      • 2019-12-06
      • 1970-01-01
      相关资源
      最近更新 更多