创建时出现Android SQLiteException [英] Android SQLiteException at create

查看:59
本文介绍了创建时出现Android SQLiteException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我想说我是android的新手,所以如果问题是愚蠢的,我深表歉意.

first I want to say I'm new at android, so I'm apologizing if the question is to stupid.

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

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)在表格"附近:语法错误

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:致命例外:ModernAsyncTask#1

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:由以下原因引起:android.database.sqlite.SQLiteException:在表格附近":语法错误(代码1):、编译时:创建表密码(_id整数主键自动递增,名称文本不为空,用户文本,密码文本,url文本,表文本不为空);

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?

对不起,我的英语.

代码在这里:

第一个表的合同类:

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)

推荐答案

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

table is an SQL keyword and you cannot use it as such as a column name.

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

Change the value of your KEY_PASS_TABLE to something else, e.g. "_table".

这篇关于创建时出现Android SQLiteException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆