表***没有名为***的列

时间:2014-07-31 18:18:10

标签: android database eclipse

Hello Guys我有一个数据库并创建了一个新的编辑文本字段来保存我的数据库名为COLUMN_AUSWIRKUNGEN的新列...如果我在我的设备上转到我的应用程序,输入文本并单击确认按钮,错误记录给我这个:

07-31 16:19:23.368: E/SQLiteLog(10039): (1) table todo has no column named auswirkungen
07-31 16:19:23.378: E/SQLiteDatabase(10039): Error inserting summary=texthere category=Sehr Wichtig (Folgenschwer bei Versagen) description=text here too auswirkungen=here is the column that dont work
07-31 16:19:23.378: E/SQLiteDatabase(10039): android.database.sqlite.SQLiteException: table todo has no column named auswirkungen (code 1): , while compiling: INSERT INTO todo(summary,category,description,auswirkungen) VALUES (?,?,?,?)
07-31 16:19:23.378: E/SQLiteDatabase(10039):    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
07-31 16:19:23.378: E/SQLiteDatabase(10039):    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:923)
07-31 16:19:23.378: E/SQLiteDatabase(10039):    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:534)
07-31 16:19:23.378: E/SQLiteDatabase(10039):    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
07-31 16:19:23.378: E/SQLiteDatabase(10039):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
07-31 16:19:23.378: E/SQLiteDatabase(10039):    at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
07-31 16:19:23.378: E/SQLiteDatabase(10039):    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1523)
07-31 16:19:23.378: E/SQLiteDatabase(10039):    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1395)
07-31 16:19:23.378: E/SQLiteDatabase(10039):    at de.vogella.android.todos.contentprovider.MyTodoContentProvider.insert(MyTodoContentProvider.java:86)
07-31 16:19:23.378: E/SQLiteDatabase(10039):    at android.content.ContentProvider$Transport.insert(ContentProvider.java:232)
07-31 16:19:23.378: E/SQLiteDatabase(10039):    at android.content.ContentResolver.insert(ContentResolver.java:1213)
07-31 16:19:23.378: E/SQLiteDatabase(10039):    at de.vogella.android.todos.TodoDetailActivity.saveState(TodoDetailActivity.java:115)
07-31 16:19:23.378: E/SQLiteDatabase(10039):    at de.vogella.android.todos.TodoDetailActivity.onPause(TodoDetailActivity.java:95)
07-31 16:19:23.378: E/SQLiteDatabase(10039):    at android.app.Activity.performPause(Activity.java:5442)
07-31 16:19:23.378: E/SQLiteDatabase(10039):    at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1356)
07-31 16:19:23.378: E/SQLiteDatabase(10039):    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3601)
07-31 16:19:23.378: E/SQLiteDatabase(10039):    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3570)
07-31 16:19:23.378: E/SQLiteDatabase(10039):    at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3547)
07-31 16:19:23.378: E/SQLiteDatabase(10039):    at android.app.ActivityThread.access$1000(ActivityThread.java:156)
07-31 16:19:23.378: E/SQLiteDatabase(10039):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1372)
07-31 16:19:23.378: E/SQLiteDatabase(10039):    at android.os.Handler.dispatchMessage(Handler.java:102)
07-31 16:19:23.378: E/SQLiteDatabase(10039):    at android.os.Looper.loop(Looper.java:157)
07-31 16:19:23.378: E/SQLiteDatabase(10039):    at android.app.ActivityThread.main(ActivityThread.java:5872)
07-31 16:19:23.378: E/SQLiteDatabase(10039):    at java.lang.reflect.Method.invokeNative(Native Method)
07-31 16:19:23.378: E/SQLiteDatabase(10039):    at java.lang.reflect.Method.invoke(Method.java:515)
07-31 16:19:23.378: E/SQLiteDatabase(10039):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:852)
07-31 16:19:23.378: E/SQLiteDatabase(10039):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:668)

07-31 16:19:23.378:E / SQLiteDatabase(10039):at dalvik.system.NativeStart.main(Native Method)

这是我的数据库

import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class TodoTable {

    // Database table
    public static final String TABLE_TODO = "todo";
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_CATEGORY = "category";
    public static final String COLUMN_SUMMARY = "summary";
    public static final String COLUMN_DESCRIPTION = "description";
    public static final String COLUMN_AUSWIRKUNGEN = "auswirkungen";
    // Database creation SQL statement
    private static final String DATABASE_CREATE = "create table " + TABLE_TODO
            + "(" + COLUMN_ID + " integer primary key autoincrement, " + ""
            + COLUMN_CATEGORY + " text not null, " + COLUMN_SUMMARY
            + " text not null," + COLUMN_DESCRIPTION + " text not null" 
            + COLUMN_AUSWIRKUNGEN+ " text not null"
            +
            ");";

    public static void onCreate(SQLiteDatabase database) {
        database.execSQL(DATABASE_CREATE);
    }

    public static void onUpgrade(SQLiteDatabase database, int oldVersion,
            int newVersion) {
        Log.w(TodoTable.class.getName(), "Upgrading database from version "
                + oldVersion + " to " + newVersion
                + ", which will destroy all old data");
        database.execSQL("DROP TABLE IF EXISTS " + TABLE_TODO);
        onCreate(database);
    }
}

内容提供商

import java.util.Arrays;
import java.util.HashSet;
import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;
import de.vogella.android.todos.database.TodoDatabaseHelper;
import de.vogella.android.todos.database.TodoTable;

public class MyTodoContentProvider extends ContentProvider {
    // database
    private TodoDatabaseHelper database;
    // Used for the UriMacher
    private static final int TODOS = 10;
    private static final int TODO_ID = 20;
    private static final String AUTHORITY = "de.vogella.android.todos.contentprovider";
    private static final String BASE_PATH = "todos";
    public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
            + "/" + BASE_PATH);
    public static final String CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE
            + "/todos";
    public static final String CONTENT_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE
            + "/todo";
    private static final UriMatcher sURIMatcher = new UriMatcher(
            UriMatcher.NO_MATCH);
    static {
        sURIMatcher.addURI(AUTHORITY, BASE_PATH, TODOS);
        sURIMatcher.addURI(AUTHORITY, BASE_PATH + "/#", TODO_ID);
    }

    @Override
    public boolean onCreate() {
        database = new TodoDatabaseHelper(getContext());
        return false;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        // Uisng SQLiteQueryBuilder instead of query() method
        SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
        // Check if the caller has requested a column which does not exists
        checkColumns(projection);
        // Set the table
        queryBuilder.setTables(TodoTable.TABLE_TODO);
        int uriType = sURIMatcher.match(uri);
        switch (uriType) {
        case TODOS:
            break;
        case TODO_ID:
            // Adding the ID to the original query
            queryBuilder.appendWhere(TodoTable.COLUMN_ID + "="
                    + uri.getLastPathSegment());
            break;
        default:
            throw new IllegalArgumentException("Unknown URI: " + uri);
        }
        SQLiteDatabase db = database.getWritableDatabase();
        Cursor cursor = queryBuilder.query(db, projection, selection,
                selectionArgs, null, null, sortOrder);
        // Make sure that potential listeners are getting notified
        cursor.setNotificationUri(getContext().getContentResolver(), uri);
        return cursor;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        int uriType = sURIMatcher.match(uri);
        SQLiteDatabase sqlDB = database.getWritableDatabase();
        int rowsDeleted = 0;
        long id = 0;
        switch (uriType) {
        case TODOS:
            id = sqlDB.insert(TodoTable.TABLE_TODO, null, values);
            break;
        default:
            throw new IllegalArgumentException("Unknown URI: " + uri);
        }
        getContext().getContentResolver().notifyChange(uri, null);
        return Uri.parse(BASE_PATH + "/" + id);
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int uriType = sURIMatcher.match(uri);
        SQLiteDatabase sqlDB = database.getWritableDatabase();
        int rowsDeleted = 0;
        switch (uriType) {
        case TODOS:
            rowsDeleted = sqlDB.delete(TodoTable.TABLE_TODO, selection,
                    selectionArgs);
            break;
        case TODO_ID:
            String id = uri.getLastPathSegment();
            if (TextUtils.isEmpty(selection)) {
                rowsDeleted = sqlDB.delete(TodoTable.TABLE_TODO,
                        TodoTable.COLUMN_ID + "=" + id, null);
            } else {
                rowsDeleted = sqlDB.delete(TodoTable.TABLE_TODO,
                        TodoTable.COLUMN_ID + "=" + id + " and " + selection,
                        selectionArgs);
            }
            break;
        default:
            throw new IllegalArgumentException("Unknown URI: " + uri);
        }
        getContext().getContentResolver().notifyChange(uri, null);
        return rowsDeleted;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
            String[] selectionArgs) {
        int uriType = sURIMatcher.match(uri);
        SQLiteDatabase sqlDB = database.getWritableDatabase();
        int rowsUpdated = 0;
        switch (uriType) {
        case TODOS:
            rowsUpdated = sqlDB.update(TodoTable.TABLE_TODO, values, selection,
                    selectionArgs);
            break;
        case TODO_ID:
            String id = uri.getLastPathSegment();
            if (TextUtils.isEmpty(selection)) {
                rowsUpdated = sqlDB.update(TodoTable.TABLE_TODO, values,
                        TodoTable.COLUMN_ID + "=" + id, null);
            } else {
                rowsUpdated = sqlDB.update(TodoTable.TABLE_TODO, values,
                        TodoTable.COLUMN_ID + "=" + id + " and " + selection,
                        selectionArgs);
            }
            break;
        default:
            throw new IllegalArgumentException("Unknown URI: " + uri);
        }
        getContext().getContentResolver().notifyChange(uri, null);
        return rowsUpdated;
    }

    private void checkColumns(String[] projection) {
        String[] available = { TodoTable.COLUMN_CATEGORY,
                TodoTable.COLUMN_SUMMARY, TodoTable.COLUMN_DESCRIPTION,
                TodoTable.COLUMN_ID ,TodoTable.COLUMN_AUSWIRKUNGEN};
        if (projection != null) {
            HashSet<String> requestedColumns = new HashSet<String>(
                    Arrays.asList(projection));
            HashSet<String> availableColumns = new HashSet<String>(
                    Arrays.asList(available));
            // Check if all columns which are requested are available
            if (!availableColumns.containsAll(requestedColumns)) {
                throw new IllegalArgumentException(
                        "Unknown columns in projection");
            }
        }
    }
}

**在手机上重新安装应用后,我收到此错误**

07-31 20:23:29.257: W/asset(2255): Copying FileAsset 0x780f87a8 (zip:/data/app/de.vogella.android.todos-2.apk:/resources.arsc) to buffer size 3392 to make it aligned.
07-31 20:23:29.337: E/SQLiteLog(2255): (1) near "nullauswirkungen": syntax error
07-31 20:23:29.337: W/dalvikvm(2255): threadid=11: thread exiting with uncaught exception (group=0x41b2ee18)
07-31 20:23:29.347: E/AndroidRuntime(2255): FATAL EXCEPTION: AsyncTask #1
07-31 20:23:29.347: E/AndroidRuntime(2255): Process: de.vogella.android.todos, PID: 2255
07-31 20:23:29.347: E/AndroidRuntime(2255): java.lang.RuntimeException: An error occured while executing doInBackground()
07-31 20:23:29.347: E/AndroidRuntime(2255):     at android.os.AsyncTask$3.done(AsyncTask.java:300)
07-31 20:23:29.347: E/AndroidRuntime(2255):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
07-31 20:23:29.347: E/AndroidRuntime(2255):     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
07-31 20:23:29.347: E/AndroidRuntime(2255):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
07-31 20:23:29.347: E/AndroidRuntime(2255):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
07-31 20:23:29.347: E/AndroidRuntime(2255):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
07-31 20:23:29.347: E/AndroidRuntime(2255):     at java.lang.Thread.run(Thread.java:864)
07-31 20:23:29.347: E/AndroidRuntime(2255): Caused by: android.database.sqlite.SQLiteException: near "nullauswirkungen": syntax error (code 1): , while compiling: create table todo(_id integer primary key autoincrement, category text not null, summary text not null,description text not nullauswirkungen text not null);
07-31 20:23:29.347: E/AndroidRuntime(2255):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
07-31 20:23:29.347: E/AndroidRuntime(2255):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:923)
07-31 20:23:29.347: E/AndroidRuntime(2255):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:534)
07-31 20:23:29.347: E/AndroidRuntime(2255):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
07-31 20:23:29.347: E/AndroidRuntime(2255):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
07-31 20:23:29.347: E/AndroidRuntime(2255):     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
07-31 20:23:29.347: E/AndroidRuntime(2255):     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1728)
07-31 20:23:29.347: E/AndroidRuntime(2255):     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1659)
07-31 20:23:29.347: E/AndroidRuntime(2255):     at de.vogella.android.todos.database.TodoTable.onCreate(TodoTable.java:23)
07-31 20:23:29.347: E/AndroidRuntime(2255):     at de.vogella.android.todos.database.TodoDatabaseHelper.onCreate(TodoDatabaseHelper.java:18)
07-31 20:23:29.347: E/AndroidRuntime(2255):     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
07-31 20:23:29.347: E/AndroidRuntime(2255):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
07-31 20:23:29.347: E/AndroidRuntime(2255):     at de.vogella.android.todos.contentprovider.MyTodoContentProvider.query(MyTodoContentProvider.java:65)
07-31 20:23:29.347: E/AndroidRuntime(2255):     at android.content.ContentProvider.query(ContentProvider.java:869)
07-31 20:23:29.347: E/AndroidRuntime(2255):     at android.content.ContentProvider$Transport.query(ContentProvider.java:212)
07-31 20:23:29.347: E/AndroidRuntime(2255):     at android.content.ContentResolver.query(ContentResolver.java:476)
07-31 20:23:29.347: E/AndroidRuntime(2255):     at android.content.CursorLoader.loadInBackground(CursorLoader.java:65)
07-31 20:23:29.347: E/AndroidRuntime(2255):     at android.content.CursorLoader.loadInBackground(CursorLoader.java:43)
07-31 20:23:29.347: E/AndroidRuntime(2255):     at android.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:312)
07-31 20:23:29.347: E/AndroidRuntime(2255):     at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:69)
07-31 20:23:29.347: E/AndroidRuntime(2255):     at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:57)
07-31 20:23:29.347: E/AndroidRuntime(2255):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
07-31 20:23:29.347: E/AndroidRuntime(2255):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-31 20:23:29.347: E/AndroidRuntime(2255):     ... 3 more
07-31 20:23:29.367: I/Adreno-EGL(2255): <qeglDrvAPI_eglInitialize:385>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_KK_3.5_RB1.04.04.02.006.066_msm8974_refs/tags/AU_LINUX_ANDROID_KK_3.5_RB1.04.04.02.006.066__release_AU ()
07-31 20:23:29.367: I/Adreno-EGL(2255): OpenGL ES Shader Compiler Version: E031.24.00.06
07-31 20:23:29.367: I/Adreno-EGL(2255): Build Date: 02/06/14 Thu
07-31 20:23:29.367: I/Adreno-EGL(2255): Local Branch: 
07-31 20:23:29.367: I/Adreno-EGL(2255): Remote Branch: refs/tags/AU_LINUX_ANDROID_KK_3.5_RB1.04.04.02.006.066
07-31 20:23:29.367: I/Adreno-EGL(2255): Local Patches: NONE
07-31 20:23:29.367: I/Adreno-EGL(2255): Reconstruct Branch: NOTHING

1 个答案:

答案 0 :(得分:0)

这是错误的:

" text not null" 
        + COLUMN_AUSWIRKUNGEN+

你可以看到你在null之后错过了一个逗号。它应该是:

" text not null, " 
        + COLUMN_AUSWIRKUNGEN+

否则表格不会被创建。

顺便说一句,这个+ "" 完全无用并引入了一些额外的工作(多一个字符串连接)