0

我正在开发一个在 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);
    }
}
4

1 回答 1

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 小部件一起使用。他们都希望这是主键的名称(它被编码到小部件中,因此当您执行列表之类的操作时,他们可以识别哪些数据来自哪些记录)。

最后一点,使用 id 的功能也更容易,autoincrement这样您就不必手动管理主键。

于 2012-07-07T12:14:40.170 回答