尝试创建表时出现SQLException

时间:2016-02-27 06:02:12

标签: android sqlite

我正在尝试向我的sqlite数据库添加一个表。

public class PendingOrdersDbHandler extends SQLiteOpenHelper {

    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "vaighai";

    // notifications table name
    private static final String TABLE_PENDINGORDERS = "pendingOrders";

    // pending orders Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_SHIPMENT_NO = "shipmentNo";
    private static final String KEY_SHIPMENT_REF_NO = "shipmentRefNo";
    private static final String KEY_PRO_INV_NO = "proformoInvoiceNumber";
    private static final String KEY_PRO_INV_DATE = "proformoInvoiceDate";
    private static final String KEY_SHIPMENT_DATE = "shipmentDate";
    private static final String KEY_PT_OF_CONTACT = "pointOfContact";
    private static final String KEY_PRODUCT = "product";
    private static final String KEY_QTY = "quantity";
    private static final String KEY_RATE = "rate";
    private static final String KEY_AMOUNT = "amount";
    private static final String KEY_PRODUCT_IMG = "productImage";
    private static final String KEY_SHIPMENT_STAT = "shipmentStatus";

    public PendingOrdersDbHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    public PendingOrdersDbHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        String CREATE_CONTACTS_TABLE = "CREATE TABLE if not exists " + TABLE_PENDINGORDERS + "("
                + KEY_ID + " INTEGER PRIMARY KEY,"
                + KEY_SHIPMENT_NO + " TEXT,"
                + KEY_SHIPMENT_REF_NO + " TEXT"
                + KEY_PRO_INV_NO + " TEXT,"
                + KEY_PRO_INV_DATE + " TEXT,"
                + KEY_SHIPMENT_DATE + " TEXT,"
                + KEY_PT_OF_CONTACT + " TEXT,"
                + KEY_PRODUCT + " TEXT,"
                + KEY_QTY + " TEXT,"
                + KEY_RATE + " TEXT,"
                + KEY_AMOUNT + " TEXT,"
                + KEY_PRODUCT_IMG + " TEXT,"
                + KEY_SHIPMENT_STAT + " TEXT,"
                + ")";
        db.execSQL(CREATE_CONTACTS_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_PENDINGORDERS);

        // Create tables again
        onCreate(db);
    }

    /**
     * CRUD(Create, Read, Update, Delete) Operations
     */

    // Adding new contact

    // Adding new pending order
    public void addPendingOrders(PendingOrdersDao pendingOrders) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();

        values.put(KEY_ID,pendingOrders.getId());
        values.put(KEY_SHIPMENT_NO, pendingOrders.getSipmentNo());
        values.put(KEY_SHIPMENT_REF_NO, pendingOrders.getShipmentRefNo());
        values.put(KEY_PRO_INV_NO, pendingOrders.getProformoInvoiceNumber());
        values.put(KEY_PRO_INV_DATE, pendingOrders.getProformoInvoiceDate());
        values.put(KEY_SHIPMENT_DATE, pendingOrders.getShipmentDate());
        values.put(KEY_PT_OF_CONTACT, pendingOrders.getPointOfContact());
        values.put(KEY_PRODUCT, pendingOrders.getProduct());
        values.put(KEY_QTY, pendingOrders.getQuantity());
        values.put(KEY_RATE,pendingOrders.get_rate());
        values.put(KEY_AMOUNT, pendingOrders.getAmount());
        values.put(KEY_PRODUCT_IMG, pendingOrders.getProductImage());
        values.put(KEY_SHIPMENT_STAT, pendingOrders.getShipmentStatus());

        // Inserting Row
        db.insert(TABLE_PENDINGORDERS, null, values);
        db.close(); // Closing database connection
    }


}

我发布了用于创建表的代码。 例外

android.database.sqlite.SQLiteException: near ")": syntax error (code 1): , while compiling: CREATE TABLE pendingOrders(id INTEGER PRIMARY KEY,shipmentNo TEXT,shipmentRefNo TEXTproformoInvoiceNumber TEXT,proformoInvoiceDate TEXT,shipmentDate TEXT,pointOfContact TEXT,product TEXT,quantity TEXT,rate TEXT,amount TEXT,productImage TEXT,shipmentStatus TEXT,)

它无法在数据库中创建表pendingOrders。我也尝试添加sqlite的'check if'命令以检查表是否已经存在并创建表(如果不存在)。但问题仍然存在。

5 个答案:

答案 0 :(得分:3)

你错过了一个逗号。将字符串+ KEY_SHIPMENT_REF_NO + " TEXT"替换为+ KEY_SHIPMENT_REF_NO + " TEXT,"

答案 1 :(得分:2)

我认为问题,或者至少一个问题是,这里似乎缺少逗号TEXTproformoInvoiceNumber我也认为最后一栏后面的逗号可能会导致问题。

答案 2 :(得分:2)

尝试用KEY_SHIPMENT_REF_NO + " TEXT"替换语句KEY_SHIPMENT_REF_NO + " TEXT," 逗号(,)在那里丢失了。

答案 3 :(得分:2)

你可以这.................

db.execSQL("CREATE TABLE " + TABLE_PHONENO + " (" + KEY_ROWID
                + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_SHIPMENT_NO + " TEXT,"
            + KEY_SHIPMENT_REF_NO + " TEXT,"
            + KEY_PRO_INV_NO + " TEXT,"
            + KEY_PRO_INV_DATE + " TEXT,"
            + KEY_SHIPMENT_DATE + " TEXT,"
            + KEY_PT_OF_CONTACT + " TEXT,"
            + KEY_PRODUCT + " TEXT,"
            + KEY_QTY + " TEXT,"
            + KEY_RATE + " TEXT,"
            + KEY_AMOUNT + " TEXT,"
            + KEY_PRODUCT_IMG + " TEXT,"
            + KEY_SHIPMENT_STAT + " TEXT); ");

答案 4 :(得分:1)

将您的查询修改为:

@Override
    public void onCreate(SQLiteDatabase db) {

        String CREATE_CONTACTS_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_PENDINGORDERS + "("
                + KEY_ID + " INT PRIMARY KEY NOT NULL,"
                + KEY_SHIPMENT_NO + " TEXT NOT NULL,"
                + KEY_SHIPMENT_REF_NO + " TEXT NOT NULL,"
                + KEY_PRO_INV_NO + " TEXT NOT NULL,"
                + KEY_PRO_INV_DATE + " TEXT NOT NULL,"
                + KEY_SHIPMENT_DATE + " TEXT NOT NULL,"
                + KEY_PT_OF_CONTACT + " TEXT NOT NULL,"
                + KEY_PRODUCT + " TEXT NOT NULL,"
                + KEY_QTY + " TEXT NOT NULL,"
                + KEY_RATE + " TEXT NOT NULL,"
                + KEY_AMOUNT + " TEXT NOT NULL,"
                + KEY_PRODUCT_IMG + " TEXT NOT NULL,"
                + KEY_SHIPMENT_STAT + " TEXT NOT NULL"
                + ")";
        db.execSQL(CREATE_CONTACTS_TABLE);
    }