我正在尝试使用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
答案 0 :(得分:1)
您似乎在尝试在实际创建表之前查询数据。尝试使用SQLiteOpenHelper创建一个表 - 你应该覆盖onCreate()并在那里执行DATABASE_CREATE
一个非常好的教程:http://www.vogella.de/articles/AndroidSQLite/article.html