Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I got error for creating sqlite db in android.
My code is:
public static final String TABLE_NAME = "user";
public static final String COL1_USER_ID = "USER_ID";
public static final String COL2_USER_NAME = "USER_NAME";
public static final String COL3_IMEI_CODE = "IMEI_CODE";
public static final String COL4_REGISTRATION_ID = "REGISTRATION_ID";
public static final String COL5_MOBILE_NUMBER = "MOBILE_NUMBER";
public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "(" + _ID + " INTEGER PRIMARY KEY,"+ COL1_USER_ID + TEXT_TYPE + COMMA_SEP + COL2_USER_NAME + TEXT_TYPE
+ COMMA_SEP + COL3_IMEI_CODE + TEXT_TYPE + COMMA_SEP + COL4_REGISTRATION_ID + VARCHAR_TYPE + COMMA_SEP + COL5_MOBILE_NUMBER + TEXT_TYPE + COMMA_SEP + ")";
Error message is:
android.database.sqlite.SQLiteException: near ")": syntax error (code 1): , while compiling: CREATE TABLE user(_id INTEGER PRIMARY KEY,USER_ID TEXT,USER_NAME TEXT,IMEI_CODE TEXT,REGISTRATION_ID VARCHAR(255),MOBILE_NUMBER TEXT,)
How to solve this?
You should remove + COMMA_SEP at last
Corrected
String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "(" + _ID + " INTEGER PRIMARY KEY,"+ COL1_USER_ID + TEXT_TYPE + COMMA_SEP + COL2_USER_NAME + TEXT_TYPE
+ COMMA_SEP + COL3_IMEI_CODE + TEXT_TYPE + COMMA_SEP + COL4_REGISTRATION_ID + VARCHAR_TYPE + COMMA_SEP + COL5_MOBILE_NUMBER + TEXT_TYPE + ")";
Replace below lines of code instead of yours.
public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "(" + _ID + " INTEGER PRIMARY KEY,"+ COL1_USER_ID + TEXT_TYPE + COMMA_SEP + COL2_USER_NAME + TEXT_TYPE
+ COMMA_SEP + COL3_IMEI_CODE + TEXT_TYPE + COMMA_SEP + COL4_REGISTRATION_ID + VARCHAR_TYPE + COMMA_SEP + COL5_MOBILE_NUMBER + TEXT_TYPE + ")";
There is one more comma at last, actually there is no need of it.
Related
Im unable to create SQLite table.
Android Studio IDE shows me error on this line "Column constrains or Comma expected, got Text"
String CREATE_TABLE_CONTACTS = "CREATE TABLE " + Util.TABLE_NAME + "(" +
Util.KEY_ID + " INTEGER PRIMARY KEY," + Util.KEY_NAME + " TEXT," +
Util.KEY_PHONE_NUMBER + " TEXT" + ")";
IMO your code
String CREATE_TABLE_CONTACTS = "CREATE TABLE " + Util.TABLE_NAME + "(" +
Util.KEY_ID + " INTEGER PRIMARY KEY," + Util.KEY_NAME + " TEXT," +
Util.KEY_PHONE_NUMBER + " TEXT" + ")";
is ok problems belongs from Util.TABLE_NAME or Util.KEY_ID check here these key strings not having spaces
DO => user_name
DON'T => user name
If your columns happen to have whitespace or other reserved words, then you may try escaping them using double quotes:
String CREATE_TABLE_CONTACTS = "CREATE TABLE \"" + Util.TABLE_NAME + "\" (" +
"\"" + Util.KEY_ID + "\" INTEGER PRIMARY KEY, \"" + Util.KEY_NAME + "\" TEXT, \"" +
Util.KEY_PHONE_NUMBER + "\" TEXT" + ")";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "addToCart_db";
public static final String TABLE_NAME = "addToCart";
public static final String COLUMN_ID = "id";
public static final String COLUMN_PRODUCT_ID = "product_id";
public static final String COLUMN_QTY = "qty";
public static final String COLUMN_PRICE = "price";
public static final String CREATE_TABLE =
"CREATE TABLE " + TABLE_NAME + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ COLUMN_PRODUCT_ID + " TEXT,"
+ COLUMN_PRICE + " TEXT,"
+COLUMN_QTY +" TEXT)";
try it, this formatting create table
I have researched other instances of this error all morning but nothing i've tried so far has worked. It could be something obvious as I am quite new to this.
I am trying to return the results of the GET_ALL_INGREDIENTS query. Which I will then put into a 2d array, but you don't need to worry about that. My TABLE_RECIPES table works fine.
SQLiteException: no such table: TABLE_INGREDIENTS (code 1): , while compiling: SELECT INGR...etc....
Which occurs on the last line of this bit of code:
public String[][] getRecipeIngredients(int id) {
String GET_ALL_INGREDIENTS = "SELECT INGREDIENT_NAME, INGREDIENT_QUANTITY, INGREDIENT_UNIT " +
"FROM TABLE_INGREDIENTS " +
"INNER JOIN TABLE_RECIPE_INGREDIENTS ON TABLE_RECIPE_INGREDIENTS.INGREDIENT_ID=TABLE_INGREDIENTS.INGREDIENT_ID " +
"WHERE RECIPE_INGREDIENTS.RECIPE_ID=" + id;
db = new MyDBHandler(mContext);
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(GET_ALL_INGREDIENTS, null);
Table Creation:
final String CREATE_TABLE_RECIPES = "CREATE TABLE IF NOT EXISTS " +
TABLE_RECIPES + "(" +
RECIPE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
RECIPE_NAME + " TEXT, " +
RECIPE_INSTRUCTIONS + " TEXT " +
")";
final String CREATE_TABLE_INGREDIENTS = "CREATE TABLE IF NOT EXISTS " +
TABLE_INGREDIENTS + "(" +
INGREDIENT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
INGREDIENT_NAME + " TEXT NOT NULL UNIQUE " +
")";
final String CREATE_TABLE_RECIPE_INGREDIENTS = "CREATE TABLE IF NOT EXISTS " +
TABLE_RECIPE_INGREDIENTS + "(" +
RECIPE_ID + " INTEGER NOT NULL, " +
INGREDIENT_ID + " INTEGER NOT NULL, " +
INGREDIENT_QUANTITY + " REAL, " +
INGREDIENT_UNIT + " TEXT, " +
"PRIMARY KEY (" + RECIPE_ID + "," + INGREDIENT_ID + "), " +
"FOREIGN KEY (" + RECIPE_ID + ") REFERENCES " + TABLE_RECIPES + "(" + RECIPE_ID + "), " +
"FOREIGN KEY (" + INGREDIENT_ID + ") REFERENCES " + TABLE_INGREDIENTS + "(" + INGREDIENT_ID + ")" +
")";
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_RECIPES);
db.execSQL(CREATE_TABLE_INGREDIENTS);
db.execSQL(CREATE_TABLE_RECIPE_INGREDIENTS);
}
Thank you for reading and I really appreciate any help i can get with this.
TABLE_INGREDIENTS is a variable. It's value is likely not "TABLE_INGREDIENTS" that is a string literal. So you might want to change
"FROM TABLE_INGREDIENTS " +
to
"FROM " + TABLE_INGREDIENTS +
(You have a number of other similar issues in your SQL too.)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am trying to create a table in DB, but I am getting SQLite exception.I have tried almost everything to fix it, but of no use.I dont know why am I getting this error. Removing comma at the end gives another exception : android.database.sqlite.SQLiteException: table products has no column named _cost (code 1):
Any suggestion is welcome.
how do I fix it ?
This is my DB handler class :
public class MyDBHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 4;
private static final String DATABASE_NAME= "products.db";
private static final String TABLE_PRODUCTS = "products";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_PRODUCTNAME = "productname";
private static final String COLUMN_VENUE = "venue";
private static final String COLUMN_DATEOFSTART = "_dateofstart";
private static final String COLUMN_DATEOFEND = "_dateofend";
private static final String DURATION_WORKSHOP= "_duration";
private static final String COST_WORKSHOP = "_cost";
public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context,DATABASE_NAME, factory,DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_PRODUCTS + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_PRODUCTNAME + " TEXT, " +
COLUMN_VENUE + " TEXT, " +
COLUMN_DATEOFSTART + " TEXT, " +
COLUMN_DATEOFEND + " TEXT, " +
DURATION_WORKSHOP + "INTEGER, " +
COST_WORKSHOP + "INTEGER, " +
");" ;
db.execSQL(query);
}
#Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS);
onCreate(db);
}
public void addProduct (Products products){
ContentValues values = new ContentValues();
values.put(COLUMN_PRODUCTNAME,products.get_productname());
values.put(COLUMN_VENUE,products.get_venue());
values.put(COLUMN_DATEOFSTART,products.get_dateofstart());
values.put(COLUMN_DATEOFEND,products.get_dateofend());
values.put(DURATION_WORKSHOP,products.get_duration());
values.put(COST_WORKSHOP,products.get_cost());
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_PRODUCTS,null,values);
db.close();
}
LOGCAT :
Caused by: android.database.sqlite.SQLiteException: near ")": syntax error (code 1): , while compiling: CREATE TABLE products(_id INTEGER PRIMARY KEY AUTOINCREMENT, productname TEXT, venue TEXT, _dateofstart TEXT, _dateofend TEXT, _durationINTEGER, _costINTEGER, );
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1675)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1606)
at com.example.dell_1.sqlite.MyDBHandler.onCreate(MyDBHandler.java:38)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at com.example.dell_1.sqlite.MyDBHandler.addProduct(MyDBHandler.java:57)
at com.example.dell_1.sqlite.MainActivity.onCreate(MainActivity.java:24)
at android.app.Activity.performCreate(Activity.java:6705)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Line 38 is db.execSQL(query); and line 57 is SQLiteDatabase db = getWritableDatabase();
change your query to this , you're missing some spaces and also you must remove the last comma from the query
#Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_PRODUCTS + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_PRODUCTNAME + " TEXT, " +
COLUMN_VENUE + " TEXT, " +
COLUMN_DATEOFSTART + " TEXT, " +
COLUMN_DATEOFEND + " TEXT, " +
DURATION_WORKSHOP + " INTEGER, " +
COST_WORKSHOP + " INTEGER " +
");" ;
db.execSQL(query);
}
replace your query with this
String query = "CREATE TABLE " + TABLE_PRODUCTS + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_PRODUCTNAME + " TEXT, " +
COLUMN_VENUE + " TEXT, " +
COLUMN_DATEOFSTART + " TEXT, " +
COLUMN_DATEOFEND + " TEXT, " +
DURATION_WORKSHOP + "INTEGER, " +
COST_WORKSHOP + "INTEGER " +
");" ;
actually you are putting one extra , at the end of query.
Change your query to this
String query = "CREATE TABLE " + TABLE_PRODUCTS + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_PRODUCTNAME + " TEXT, " +
COLUMN_VENUE + " TEXT, " +
COLUMN_DATEOFSTART + " TEXT, " +
COLUMN_DATEOFEND + " TEXT, " +
DURATION_WORKSHOP + " INTEGER, " +
COST_WORKSHOP + " INTEGER " +
");" ;
You have formatting errors in there. An extra comma and two column names concatenated with their column types. See for e.g. how _costINTEGER should be _cost INTEGER
Change AUTOINCREMENT to AUTO_INCREMENT and remove extra ',' after column COST_WORKSHOP
String query = "CREATE TABLE " + TABLE_PRODUCTS + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTO_INCREMENT, " +
COLUMN_PRODUCTNAME + " TEXT, " +
COLUMN_VENUE + " TEXT, " +
COLUMN_DATEOFSTART + " TEXT, " +
COLUMN_DATEOFEND + " TEXT, " +
DURATION_WORKSHOP + " INTEGER, " +
COST_WORKSHOP + " INTEGER " +
");" ;
I am getting error like "SQLite exception near ") ":syntax error (code 1)
here is my create table command
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "attendance.db";
private static final String TEXT_TYPE = " TEXT";
private static final String COMMA_SEP = " ,";
private static final String SQL_CREATE_ENTRIES = "CREATE TABLE " +
TableElement.TABLE_NAME + " (" +
TableElement.ID_COLUM + " INTEGER PRIMARY KEY AUTOINCREMENT ," +
TableElement.IMAGE_COLUM + TEXT_TYPE + COMMA_SEP +
TableElement.NAME_COLUM + TEXT_TYPE + COMMA_SEP +
TableElement.GARDIAN_NAME_COLUM + TEXT_TYPE + COMMA_SEP +
TableElement.CONTRACT_COLUM + TEXT_TYPE + COMMA_SEP +
TableElement.PRESENT_COLUM + TEXT_TYPE + COMMA_SEP +
TableElement.ABSENT_COLUM + TEXT_TYPE + COMMA_SEP +
TableElement.LATE_COLUM + TEXT_TYPE + COMMA_SEP +
TableElement.CHECKED_BUTTON_COLUM + TEXT_TYPE + COMMA_SEP +
" )";
You do not need the last COMMA_SEP.
When in doubt, print the SQL query out and these syntax errors become more visible. In this instance, the ) in the error message suggests the error is close to the end.
Other thoughts:
Some of these static variables, eg. COMMA_SEP, are making your code harder to read.
You have mis-spelt COLUMN on your column name attributes.
private static final String CREATE_TABLE_USER = "CREATE TABLE "
+ TABLE_USER + "(" + "id" + " INTEGER PRIMARY KEY," + "username"
+ " TEXT," + "passwort" + " TEXT," + "sportlevel" + " TEXT,"
+ "ziel" + " TEXT," + "alter" + " REAL," + "groesse" + " REAL,"
+ "gewicht" + " REAL," + "zielgewicht" + " REAL,"
+ "koerperfettanteil" + " REAL," + "trainingprowoche" + " REAL"
+ ")";
//create table statement for USERs
You can not use the alter as column name in the SQLite because it is already reserved keyword.
See here for reserved keywords : https://sqlite.org/lang_keywords.html
alter is a sqlite keyword, I do not think it is usable as column name