我正在尝试使用android创建数据库,但我不断收到错误消息,指出我的列名无效,我不知道为什么。

我的代码有点长,我使用了一个在网上找到的模板示例(我也只是使用其自己的值尝试了该示例,并且可以正常工作),而且我不确定引起错误的位是哪一个。

编辑:我应该已经发布了另一个创建表的类,抱歉,我认为这可能是发生问题的地方(原始帖子可能是相关的,所以我将其留在底部

package com.database.coffee;

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

public class CoffeeTable {
    // Database creation SQL statement
    private static final String DATABASE_CREATE = "create table coffee "
            + "(_id integer primary key autoincrement, "
            + "name text not null, " + "address text not null, "
            + "postcode text not null, " + "phone text not null, "
            + "distance text not null, " + "website 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(CoffeeTable.class.getName(), "Upgrading database from version "
                + oldVersion + " to " + newVersion
                + ", which will destroy all old data");
        database.execSQL("DROP TABLE IF EXISTS coffee");
        onCreate(database);
    }
}

我的代码是:

package com.database.coffee;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class CoffeeDBAdapter {

// Database fields
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "name";
public static final String KEY_ADDRESS = "address";
public static final String KEY_POSTCODE = "postcode";
public static final String KEY_PHONE = "phone";
public static final String KEY_DISTANCE = "distance";
public static final String KEY_WEBSITE = "website";
private static final String DB_TABLE = "coffee";
private Context context;
private SQLiteDatabase db;
private CoffeeDatabaseHelper dbHelper;

public CoffeeDBAdapter(Context context) {
    this.context = context;
}

public CoffeeDBAdapter open() throws SQLException {
    dbHelper = new CoffeeDatabaseHelper(context);
    db = dbHelper.getWritableDatabase();
    return this;
}

public void close() {
    dbHelper.close();
}



public long createCoffee(String name, String address, String postcode, String phone,
                         String distance, String website) {
    ContentValues values = createContentValues(name, address, postcode, phone, distance, website);

    return db.insert(DB_TABLE, null, values);
}



public boolean updateCoffee(long rowId, String name, String address, String postcode,
                            String phone, String distance, String website) {
    ContentValues values = createContentValues(name, address, postcode, phone, distance, website);

    return db.update(DB_TABLE, values, KEY_ROWID + "=" + rowId, null) > 0;
}


public boolean deleteCoffee(long rowId) {
    return db.delete(DB_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}



public Cursor fetchAllCoffee() {
    return db.query(DB_TABLE, new String[] { KEY_ROWID, KEY_NAME, KEY_ADDRESS, KEY_POSTCODE, KEY_PHONE, KEY_DISTANCE, KEY_WEBSITE }, null, null, null, null, null);
}



public Cursor fetchCoffee(long rowId) throws SQLException {
    Cursor mCursor = db.query(true, DB_TABLE, new String[] { KEY_ROWID,KEY_NAME, KEY_ADDRESS, 
                                                             KEY_POSTCODE, KEY_PHONE, KEY_DISTANCE, 
                                                             KEY_WEBSITE}, KEY_ROWID + "=" + rowId, null, null, null, null, null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

private ContentValues createContentValues(String name, String address, String postcode, String phone, String distance, String website) {
    ContentValues values = new ContentValues();
    values.put(KEY_NAME, name);
    values.put(KEY_ADDRESS, address);
    values.put(KEY_POSTCODE, postcode);
    values.put(KEY_PHONE, phone);
    values.put(KEY_DISTANCE, distance);
    values.put(KEY_WEBSITE, website);
    return values;
}

在此先感谢您的帮助。

编辑2:

该错误的logcat日志是:

 12-11 21:50:42.356: ERROR/AndroidRuntime(365): FATAL EXCEPTION: main
 12-11 21:50:42.356: ERROR/AndroidRuntime(365): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.menus.app/com.menus.app.FindFood}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.menus.app/com.menus.app.CoffeeTab}: android.database.sqlite.SQLiteException: no such column: name: , while compiling: SELECT _id, name, address, postcode, phone, distance, website FROM coffee
 12-11 21:50:42.356: ERROR/AndroidRuntime(365):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
 12-11 21:50:42.356: ERROR/AndroidRuntime(365):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
 12-11 21:50:42.356: ERROR/AndroidRuntime(365):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
 12-11 21:50:42.356: ERROR/AndroidRuntime(365):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
 12-11 21:50:42.356: ERROR/AndroidRuntime(365):     at android.os.Handler.dispatchMessage(Handler.java:99)
 12-11 21:50:42.356: ERROR/AndroidRuntime(365):     at android.os.Looper.loop(Looper.java:123)
 12-11 21:50:42.356: ERROR/AndroidRuntime(365):     at android.app.ActivityThread.main(ActivityThread.java:3683)
 12-11 21:50:42.356: ERROR/AndroidRuntime(365):     at java.lang.reflect.Method.invokeNative(Native Method)
 12-11 21:50:42.356: ERROR/AndroidRuntime(365):     at java.lang.reflect.Method.invoke(Method.java:507)
 12-11 21:50:42.356: ERROR/AndroidRuntime(365):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
 12-11 21:50:42.356: ERROR/AndroidRuntime(365):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
 12-11 21:50:42.356: ERROR/AndroidRuntime(365):     at dalvik.system.NativeStart.main(Native Method)
 12-11 21:50:42.356: ERROR/AndroidRuntime(365): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.menus.app/com.menus.app.CoffeeTab}: android.database.sqlite.SQLiteException: no such column: name: , while compiling: SELECT _id, name, address, postcode, phone, distance, website FROM coffee
 12-11 21:50:42.356: ERROR/AndroidRuntime(365):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
 12-11 21:50:42.356: ERROR/AndroidRuntime(365):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:1487)
 12-11 21:50:42.356: ERROR/AndroidRuntime(365):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
 12-11 21:50:42.356: ERROR/AndroidRuntime(365):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
 12-11 21:50:42.356: ERROR/AndroidRuntime(365):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:654)
 12-11 21:50:42.356: ERROR/AndroidRuntime(365):     at android.widget.TabHost.setCurrentTab(TabHost.java:326)
 12-11 21:50:42.356: ERROR/AndroidRuntime(365):     at android.widget.TabHost.addTab(TabHost.java:216)
 12-11 21:50:42.356: ERROR/AndroidRuntime(365):     at com.menus.app.FindFood.onCreate(FindFood.java:38)
 12-11 21:50:42.356: ERROR/AndroidRuntime(365):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
 12-11 21:50:42.356: ERROR/AndroidRuntime(365):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
 12-11 21:50:42.356: ERROR/AndroidRuntime(365):     ... 11 more
 12-11 21:50:42.356: ERROR/AndroidRuntime(365): Caused by: android.database.sqlite.SQLiteException: no such column: name: , while compiling: SELECT _id, name, address, postcode, phone, distance, website FROM coffee
 12-11 21:50:42.356: ERROR/AndroidRuntime(365):     at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
 12-11 21:50:42.356: ERROR/AndroidRuntime(365):     at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
 12-11 21:50:42.356: ERROR/AndroidRuntime(365):     at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
 12-11 21:50:42.356: ERROR/AndroidRuntime(365):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
 12-11 21:50:42.356: ERROR/AndroidRuntime(365):     at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)
 12-11 21:50:42.356: ERROR/AndroidRuntime(365):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
 12-11 21:50:42.356: ERROR/AndroidRuntime(365):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1356)
 12-11 21:50:42.356: ERROR/AndroidRuntime(365):     at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1235)
 12-11 21:50:42.356: ERROR/AndroidRuntime(365):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1189)
 12-11 21:50:42.356: ERROR/AndroidRuntime(365):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1271)
 12-11 21:50:42.356: ERROR/AndroidRuntime(365):     at com.database.coffee.CoffeeDBAdapter.fetchAllCoffee(CoffeeDBAdapter.java:80)
 12-11 21:50:42.356: ERROR/AndroidRuntime(365):     at com.menus.app.CoffeeTab.fillData(CoffeeTab.java:110)
 12-11 21:50:42.356: ERROR/AndroidRuntime(365):     at com.menus.app.CoffeeTab.onCreate(CoffeeTab.java:35)
 12-11 21:50:42.356: ERROR/AndroidRuntime(365):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
  12-11 21:50:42.356: ERROR/AndroidRuntime(365):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
 12-11 21:50:42.356: ERROR/AndroidRuntime(365):     ... 20 more
 12-11 21:50:42.415: WARN/ActivityManager(69):   Force finishing activity com.menus.app/.FindFood

===============>>#1 票数:1

看来您正在尝试在实际创建表之前查询数据。 尝试使用SQLiteOpenHelper创建表-您应该重写onCreate()并在那里执行DATABASE_CREATE

一个非常不错的教程: http : //www.vogella.de/articles/AndroidSQLite/article.html

  ask by djcmm476 translate from so

未解决问题?本站智能推荐: