Reputation: 65
I have created two tables name SD_MPO, PRODUCTS, Customers, ORDMAIN and ORDITEM.Tables are created successfully and in my DatabaseHandler file, I have created Ordman class to insert data in my ORDMAIN table.I am Using Contentvalues and I checked my log and I am getting values. but when the db.insert is performed, data is not inserted in ORDMAIN table.I am able to insert the same data in Customers or SD_mpo tables but can't insert my data in ORDMAIN table. Codes are given below
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();
}
}
Upvotes: 0
Views: 82
Reputation: 11255
While creating table TABLE_ORDMAIN
you are not taking PREFIX
field. But at the time of inserting value you are using that field to insert value in it but eventually field isn't in that table.
If you want to insert the value in PREFIX
field then you need to add that field while creating table. Modify table and add that field as below.
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);
Upvotes: 4
Reputation: 31
db.insert() returns a positive integer if data is successfully inserted in the table, if not - something must have gone wrong. Hope this may help you rectify the issue.
Upvotes: 1