Reputation: 1813
I'm trying to create a database using android but I keep getting the error saying my column name is invalid and I can't figure out why.
My code is a bit long, I used a template example I found online (I also just tried the example out with it's own values and it worked fine), and I'm not sure which of the bits it is that's causing the error.
EDIT: I should have posted the other class which creates the table, apologies, I think this may be where the problem is occurring (the original post may be relevant, so I'll leave it at the bottom
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);
}
}
My code is:
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;
}
Thanks in advance for any help.
EDIT 2:
The logcat log for the error is:
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
Upvotes: 0
Views: 819
Reputation: 13506
It seems that you're trying to query the data before actually creating a table. Try using SQLiteOpenHelper to create a table - you should override onCreate() and execute DATABASE_CREATE there
A very nice tutorial: http://www.vogella.de/articles/AndroidSQLite/article.html
Upvotes: 1