创建表后,在sqlite表中插入数据无效

时间:2018-04-03 05:18:58

标签: android android-sqlite

我创建了两个表名SD_MPO,PRODUCTS,Customers,ORDMAIN和ORDITEM.Tables已成功创建,并且在我的DatabaseHandler文件中,我创建了Ordman类以在我的ORDMAIN表中插入数据。我正在使用Contentvalues并且我检查了我的记录,我正在获得价值。但是当执行db.insert时,数据不会插入ORDMAIN表中。我可以在Customers或SD_mpo表中插入相同的数据,但不能在ORDMAIN表中插入我的数据。代码如下:

package com.opl.salematrix;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

public class DatabaseHandler extends SQLiteOpenHelper {

    // Database Version
    private static final int DATABASE_VERSION = 1;
    // Database Name
    private static final String DATABASE_NAME = "localdatabase";


    // Contacts table sd_mpo
    private static final String TABLE_MPO = "SD_MPO";
    // Contacts Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_FNAME = "fname";
    private static final String KEY_SNAME = "sname";
    private static final String KEY_PH_NO = "phone_number";


    private static final String TABLE_CUSTOMERS = "CUSTOMERS";
    // Contacts Table Columns names
    private static final String c_id = "id";
    private static final String c_name = "name";
    private static final String c_cust = "customer";
    private static final String c_mpo = "mpo";


    private static final String TABLE_PRODUCTS = "PRODUCTS";

    private static final String KEY_SL = "sl";
    private static final String KEY_PRODUCT_CODE = "PRODUCT_CODE";
    private static final String KEY_PRODUCT_NAME = "PRODUCT_NAME";
    private static final String KEY_PROD_RATE = "PROD_RATE";
    private static final String KEY_PROD_VAT = "PROD_VAT";
    private static final String KEY_PROD_QUANT = "Quantity";



    private static final String TABLE_ORDMAIN = "ORDMAIN";

    private static final String ORD_ID = "id";
    private static final String ORD_NO = "ORD_NO";
    private static final String CUST_CODE = "CUST_CODE";
    private static final String MPO_CODE = "MPO_CODE";
    private static final String ORD_DATE = "ORD_DATE";
    private static final String ENTER_DT = "ENTER_DT";
    private static final String PREFIX = "PREFIX";
    private static final String EMPNO = "EMPNO";
    private static final String DELI_DATE = "DELI_DATE";
    private static final String DELI_TIME = "DELI_TIME";
    private static final String SHIFT_STAT = "SHIFT_STAT";
    private static final String PAY_MODE = "PAY_MODE";


    private static final String TABLE_ORDITEM = "ORDITEM";
    private static final String ORD_ITEM_ID = "id";
    private static final String ORD_NO_ITEM = "ORD_NO";
    private static final String P_CODE = "P_CODE";
    private static final String PROD_RATE = "PROD_RATE";
    private static final String ORD_QNTY = "ORD_QNTY";
    private static final String ENTER_DT_ITEM = "ENTER_DT";
    private static final String PROD_VAT = "PROD_VAT";

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

    //Create tables
    @Override
    public void onCreate(SQLiteDatabase db) {


        String CREATE_TABLE_CONTACTS = "CREATE TABLE " + TABLE_MPO + "("
                + KEY_ID + " INTEGER PRIMARY KEY,"
                + KEY_FNAME + " TEXT,"
                + KEY_SNAME + " TEXT,"
                + KEY_PH_NO + " TEXT" + ")";
        db.execSQL(CREATE_TABLE_CONTACTS);


        String CREATE_TABLE_PRODUCTS = "CREATE TABLE " + TABLE_PRODUCTS + "("
                + KEY_SL + " INTEGER PRIMARY KEY,"
                + KEY_PROD_QUANT + " TEXT,"
                + KEY_PRODUCT_CODE + " TEXT,"
                + KEY_PRODUCT_NAME + " TEXT,"
                + KEY_PROD_RATE + " TEXT,"
                + KEY_PROD_VAT + " TEXT" + ")";
        db.execSQL(CREATE_TABLE_PRODUCTS);


        String CREATE_TABLE_CUSTOMERS = "CREATE TABLE " + TABLE_CUSTOMERS + "("
                + c_id + " INTEGER PRIMARY KEY,"
                + c_name + " TEXT,"
                + c_cust + " TEXT,"
                + c_mpo + " TEXT" + ")";
        db.execSQL(CREATE_TABLE_CUSTOMERS);



        String CREATE_TABLE_ORDMAIN = "CREATE TABLE " + TABLE_ORDMAIN + "("
                + ORD_ID + " integer primary key autoincrement not null,"
                + ORD_NO + " TEXT,"
                + CUST_CODE + " TEXT,"
                + MPO_CODE + " TEXT,"
                + ORD_DATE + " TEXT,"
                + DELI_DATE + " TEXT,"
                + DELI_TIME + " TEXT,"
                + SHIFT_STAT + " TEXT,"
                + PAY_MODE + " TEXT" + ")";
        db.execSQL(CREATE_TABLE_ORDMAIN);



        String CREATE_TABLE_ORDITEM = "CREATE TABLE " + TABLE_ORDITEM + "("
                + ORD_ITEM_ID + " integer primary key autoincrement not null,"
                + ORD_NO_ITEM + " TEXT,"
                + P_CODE + " TEXT,"
                + PROD_RATE + " TEXT,"
                + ORD_QNTY + " TEXT,"
                + ENTER_DT_ITEM + " TEXT,"
                + PROD_VAT + " TEXT" + ")";
        db.execSQL(CREATE_TABLE_ORDITEM);



    }

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

        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_MPO);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_CUSTOMERS);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_ORDMAIN);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_ORDITEM);
        onCreate(db);

    }


    public void addProducts(Product product) {
        SQLiteDatabase db = this.getReadableDatabase();

        ContentValues values = new ContentValues();

        values.put(KEY_PRODUCT_CODE, product.getproductcode());
        values.put(KEY_PRODUCT_NAME, product.getproductname());
        values.put(KEY_PROD_RATE, product.getproductrate());
        values.put(KEY_PROD_VAT, product.getproductvat());
        db.insert(TABLE_PRODUCTS, null, values);
        db.close();
    }


    public void addCustomers(OfflineCustomer customer) {
        SQLiteDatabase db = this.getReadableDatabase();

        ContentValues values = new ContentValues();
        values.put(c_mpo, customer.getCCUST());
        values.put(c_cust, customer.getCMPO());
        values.put(c_name, customer.getCName());
        db.insert(TABLE_CUSTOMERS, null, values);
      //  db.insert(TABLE_ORDMAIN, null, values);
        db.close();
    }

    public void Ordmain(Ordmain ordmain) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();

        values.put(ORD_NO, ordmain.getordno());
        values.put(CUST_CODE, ordmain.getcustcode());
        values.put(MPO_CODE, ordmain.getmpocode());
        values.put(ORD_DATE, ordmain.getdelidate());


        values.put(DELI_DATE, ordmain.getdelidate());
        values.put(DELI_TIME, ordmain.getdelitime());
        values.put(PREFIX, ordmain.getcustcode());
        values.put(SHIFT_STAT, ordmain.getshiftstat());
        values.put(PAY_MODE, ordmain.getpaymode());

   Log.d("ORDERMAIN",""+ordmain.getordno()+ordmain.getcustcode()+ordmain.getmpocode()+ordmain.getdelidate()+ordmain.getdelitime()
 +ordmain.getshiftstat()+ordmain.getpaymode());
  db.insert(TABLE_ORDMAIN,null,values);
   db.insert(TABLE_ORDMAIN,null,values);
   db.close();
    }

    //Insert values to the table contacts
    public void addContacts(Contact contact) {

        SQLiteDatabase db = this.getReadableDatabase();
        ContentValues values = new ContentValues();

        values.put(KEY_FNAME, contact.getFName());
        values.put(KEY_SNAME, contact.getSName());

        db.insert(TABLE_MPO, null, values);
        db.close();
    }
}

2 个答案:

答案 0 :(得分:4)

创建表TABLE_ORDMAIN时,您没有使用PREFIX字段。但是在插入值时,您正在使用该字段在其中插入值,但最终字段不在该表中。

如果要在PREFIX字段中插入值,则需要在创建表时添加该字段。修改表并添加该字段,如下所示。

String CREATE_TABLE_ORDMAIN = "CREATE TABLE " + TABLE_ORDMAIN + "("
                + ORD_ID + " integer primary key autoincrement not null,"
                + ORD_NO + " TEXT,"
                + CUST_CODE + " TEXT,"
                + MPO_CODE + " TEXT,"
                + ORD_DATE + " TEXT,"
                + DELI_DATE + " TEXT,"
                + DELI_TIME + " TEXT,"
                + SHIFT_STAT + " TEXT,"
                + PREFIX + " TEXT,"
                + PAY_MODE + " TEXT" + ")";
        db.execSQL(CREATE_TABLE_ORDMAIN);

答案 1 :(得分:1)

如果数据成功插入表中,则db.insert()返回正整数,否则返回 - 必须出错。希望这可以帮助你纠正这个问题。