I am working on an application which stores some data in sqlite, while creating table android throws an exception:
Failure 1 : Syntax error near table1. on Ox12d510
I am unable to identify what I am doing wrong. Source code:
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MySQLiteHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "bedekar12.db";
public static final int DATABASE_VERSION = 1;
public static final String COLUMN_ID = "_id";
public static final String COLUMN_1 = "column1";
public static final String PRODUCT_NAME = "product_name";
public static final String PRODUCT_CATEGORY = "product_category";
public static final String ORDER_ID = "order_id";
public static final String ORDER_DATE = "order_date";
public static final String CUSTOMER_ID = "customer_id";
public static final String TOTAL = "total";
public static final String SALESMAN_ID = "salesman_id";
public static final String ORDER_STATUS = "order_status";
public static final String PRODUCT_ID = "product_id";
public static final String PACKAGE_ID = "package_id";
public static final String QUANTITY = "quantity";
public static final String PACKGAGE = "package";
public static final String WEIGHT = "weight";
public static final String PRICE = "price";
public static final String CUSTOMER_NO = "customer_no";
public static final String CUSTOMER_NAME = "customer_name";
public static final String CUSTOMER_ADD = "customer_add";
public static final String CUSTOMER_CITY = "customer_city";
public static final String CUSTOMER_REGION = "customer_region";
public static final String CUSTOMER_POSTAL = "customer_postal";
public static final String CONTACT = "contact";
public static final String CUSTOMER_TITLE = "customer_title";
public static final String CUSTOMER_PHONE = "customer_phone";
public static final String CUSTOMER_EMAIL = "customer_email";
public static final String LAT = "lat";
public static final String LNG = "lng";
public static final String EVENTS_DATE = "events_date";
public static final String EVENTS_MSG = "events_msg";
// 1. News Table
public static final String NEWS_TABLE = "news_table";
public static final String NEWS_CREATE = "create table " + NEWS_TABLE + " ( "
+ COLUMN_ID + " integer primary key , "
+ COLUMN_1 + " text not null ); ";
// 2. product table
public static final String PRODUCT_TABLE = "product_table";
public static final String PRODUCT_CREATE = "create table " + PRODUCT_TABLE + " ( "
+ COLUMN_ID + " integer primary key , "
+ PRODUCT_ID + " text not null, "
+ PRODUCT_NAME + " text not null, "
+ PRODUCT_CATEGORY + " text not null ); ";
// 3. order table
public static final String ORDER_TABLE = "order_table";
public static final String ORDER_CREATE = "create table " + ORDER_TABLE + " ( "
+ COLUMN_ID + " integer primary key, "
+ ORDER_ID + " text not null, "
+ ORDER_DATE + " text not null, "
+ CUSTOMER_ID + " text not null, "
+ TOTAL + " text not null, "
+ SALESMAN_ID + " text not null, "
+ ORDER_STATUS + " text not null ); ";
// 4. return order table
public static final String RETURN_ORDER_TABLE = "return_order_table";
public static final String RETURN_ORDER_CREATE = "create table " + ORDER_TABLE + " ( "
+ COLUMN_ID + " integer primary key, "
+ ORDER_ID + " text not null, "
+ ORDER_DATE + " text not null, "
+ CUSTOMER_ID + " text not null, "
+ TOTAL + " text not null, "
+ SALESMAN_ID + " text not null, "
+ ORDER_STATUS + " text not null ); ";
// 5. OrderDetails Table
public static final String ORDER_DETAILS_TABLE = "details_table";
public static final String ORDER_DETAILS_CREATE = "create table " + ORDER_DETAILS_TABLE + " ( "
+ COLUMN_ID + " integer primary key , "
+ ORDER_ID + " text not null, "
+ PRODUCT_ID + " text not null, "
+ PACKAGE_ID + " text not null, "
+ QUANTITY + " text not null, "
+ PRICE + " text );";
// 6. Return OrderDetails Table
public static final String RETURN_ORDER_DETAILS_TABLE = "return_details_table";
public static final String RETURN_ORDER_DETAILS_CREATE = "create table " + ORDER_DETAILS_TABLE + " ( "
+ COLUMN_ID + " integer primary key, "
+ ORDER_ID + " text not null, "
+ PRODUCT_ID + " text not null, "
+ PACKAGE_ID + " text not null, "
+ QUANTITY + " text not null, "
+ PRICE + " text );";
// 7. Package table
public static final String PACKAGING_TABLE = "packaging_table";
public static final String PACKAGING_CREATE = "create table " + PACKAGING_TABLE + " ( "
+ COLUMN_ID + " integer primary key, "
+ PRODUCT_ID + " text not null, "
+ PACKAGE_ID + " text not null, "
+ PACKGAGE + " text not null, "
+ WEIGHT + " text not null, "
+ QUANTITY + " text not null, "
+ PRICE + " text not null );";
// 8. Customer table
public static final String CUSTOMER_TABLE = "customer_table";
public static final String CUSTOMER_CREATE = "create table " + CUSTOMER_TABLE + " ( "
+ COLUMN_ID + " integer primary key, "
+ CUSTOMER_NAME + " text not null, "
+ CUSTOMER_ADD + " text not null, "
+ CUSTOMER_CITY + " text not null, "
+ CUSTOMER_REGION + " text not null, "
+ CUSTOMER_POSTAL + " text not null, "
+ CONTACT + " text not null, "
+ CUSTOMER_TITLE + " text not null, "
+ CUSTOMER_PHONE + " text not null, "
+ CUSTOMER_EMAIL + " text not null, "
+ LAT + " text not null, "
+ LNG + " text not null ); ";
// 9. Event table;
public static final String EVENTS_TABLE = "events_table";
public static final String EVENTS_CREATE = "create table " + EVENTS_TABLE + " ( "
+ COLUMN_ID + " integer primary key, "
+ EVENTS_DATE + " text not null, "
+ EVENTS_MSG + " text not null ); ";
public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(NEWS_TABLE);
db.execSQL(PRODUCT_TABLE);
db.execSQL(ORDER_TABLE);
db.execSQL(RETURN_ORDER_TABLE);
db.execSQL(ORDER_DETAILS_TABLE);
db.execSQL(RETURN_ORDER_DETAILS_TABLE);
db.execSQL(PACKAGING_TABLE);
db.execSQL(CUSTOMER_TABLE);
db.execSQL(EVENTS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int version1, int version2) {
db.execSQL("DROP TABLE IF EXISTS " + NEWS_TABLE);
db.execSQL("DROP TABLE IF EXISTS " + PRODUCT_TABLE);
db.execSQL("DROP TABLE IF EXISTS " + ORDER_TABLE);
db.execSQL("DROP TABLE IF EXISTS " + RETURN_ORDER_TABLE);
db.execSQL("DROP TABLE IF EXISTS " + ORDER_DETAILS_TABLE);
db.execSQL("DROP TABLE IF EXISTS " + RETURN_ORDER_DETAILS_TABLE);
db.execSQL("DROP TABLE IF EXISTS " + PACKAGING_TABLE);
db.execSQL("DROP TABLE IF EXISTS " + CUSTOMER_TABLE);
db.execSQL("DROP TABLE IF EXISTS " + EVENTS_TABLE);
onCreate (db);
}
}
You are using the wrong string in your
onCreatemethod… you are giving the table name only, not the string to create the tables.Change this:
To this:
As a side note, all of the primary keys should be called
_idto facilitate use with android widgets. They all expect that to be the name of the primary key (it coded into the widgets so they can identify what data comes from what record when you do things like lists).One last pointer, it is also much easier to use the
autoincrementfeature for the ids so that you don’t have to manage the primary keys manually.