2012-07-07 107 views
0

我的工作存储在sqlite的一些数据的应用程序,在创建表的Android抛出异常:sqlite的异常处理的Android

Failure 1 : Syntax error near table1. on Ox12d510 

我无法确定我在做什么错。源代码:

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); 
    } 
} 
+2

请添加您正在执行的SQL。 – 2012-07-07 11:34:51

+0

-Andrey Ermakov我已经更新了我的源代码 – Sumit 2012-07-07 11:41:20

+1

你还可以指定有多少例外的代码? – 2012-07-07 11:44:25

回答

2

您使用了错误的字符串在onCreate方法......你只给表名,不是字符串创建表。

更改此:

@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 onCreate(SQLiteDatabase db) { 
    db.execSQL(NEWS_CREATE); 
    db.execSQL(PRODUCT_CREATE); 
    db.execSQL(ORDER_CREATE); 
    db.execSQL(RETURN_ORDER_CREATE); 
    db.execSQL(ORDER_DETAILS_CREATE); 
    db.execSQL(RETURN_ORDER_DETAILS_CREATE); 
    db.execSQL(PACKAGING_CREATE); 
    db.execSQL(CUSTOMER_CREATE); 
    db.execSQL(EVENTS_CREATE); 
} 

作为一个侧面说明,所有的主键应该叫_id,以促进与Android的Widget使用。他们都希望这是主键的名称(它编码到小部件中,以便他们可以识别当您执行诸如列表之类的操作时哪些数据来自哪条记录)。

作为最后一个指针,使用autoincrement功能的ID更容易,因此您不必手动管理主键。

+0

-Barak谢谢很多人..这是一个来自myside的愚蠢的错误..非常感谢 – Sumit 2012-07-07 12:18:22

+1

没问题...自己做了几次。 – Barak 2012-07-07 12:20:01