我创建了一个数据库并将其添加到资产文件夹中。表名订单有三列id,orderItems和orderDate。在orderItems我将arraylist转换为字符串然后成功保存哪些存储为第一个记录,但是当我尝试存储第二个记录时,它显示错误。
以下是我用于在订单表中添加记录的功能。
void addOrders(Order ord) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues orderValues = new ContentValues();
orderValues.put(ORDER_ID, ord.get_orderId());
orderValues.put(ORDER_ITEMS, ord.get_orderItems());
orderValues.put(ORDER_DATE, ord.get_orderDate());
// Inserting Row
db.insert(TABLE_ORDERS_NEW, null, orderValues);
db.close(); // Closing database connection
}
以下是功能,我用它将 arraylist(selectedProducts)转换为字符串并保存到表格。
public void saveOrders(){
Gson gson = new Gson();
String arrayList = gson.toJson(selectedProducts);
db.addOrders(new Order(arrayList, formattedDate));
}
DatabaseHandler.class
公共类DatabaseHandler扩展了SQLiteAssetHelper {
// All Static variables
private static String TAG = DatabaseHandler.class.getName();
// Database Path
private static String DB_PATH = "/data/data/com.WaqasAsgharBhalli.za_traders/databases/";
private static String DB_NAME = "myDatabase.sqlite";
private SQLiteDatabase myDataBase = null;
private final Context myContext;
// Database Version
private static final int DATABASE_VERSION = 1;
// Contacts table name
private static final String TABLE_CONTACTS_NEW = "customer";
// Medicine table name
private static final String TABLE_PRODUCT_NEW = "products";
// Medicine table name
private static final String TABLE_USERLOGIN_NEW = "userlogin";
// ORDERS table name
private static final String TABLE_ORDERS_NEW = "orders";
// Contacts Table Columns name
private static final String CUSTOMER_ID = "id";
private static final String CUSTOMER_CODE = "code";
private static final String CUSTOMER_NAME = "name";
private static final String CUSTOMER_ADDRESS = "address";
private static final String CUSTOMER_LICENCECENO = "licenceNo";
// Product Table Columns name
private static final String PRODUCT_ID = "id";
private static final String PRODUCT_CODE = "pcode";
private static final String PRODUCT_NAME = "pname";
private static final String PRODUCT_PRICE = "price";
private static final String PRODUCT_QUANTITY = "quantity";
// UserLogin Table Columns name
private static final String USERLOGIN_ID = "id";
private static final String USERLOGIN_USERCODE = "usercode";
private static final String USERLOGIN_NAME = "uname";
private static final String USERLOGIN_PASSWORD = "password";
// Order Table Columns name
private static final String ORDER_ID = "id";
private static final String ORDER_ITEMS = "orderItems";
private static final String ORDER_DATE = "orderDate";
// CREATING CUSTOMER CONTACT TABLE
private static final String CREATE_CONTACTS_CUSTOMER =
"CREATE TABLE " + TABLE_CONTACTS_NEW + "("
+ CUSTOMER_ID + " INTEGER PRIMARY KEY," + CUSTOMER_CODE + " TEXT,"
+ CUSTOMER_NAME + " TEXT," + CUSTOMER_LICENCECENO + " TEXT,"
+ CUSTOMER_ADDRESS + " TEXT"+");";
// CREATING PRODUCT TABLE
private static final String CREATE_CONTACTS_PRODUCTS =
"CREATE TABLE " + TABLE_PRODUCT_NEW + "("
+ PRODUCT_ID + " INTEGER PRIMARY KEY,"+ PRODUCT_CODE + " TEXT,"
+ PRODUCT_NAME + " TEXT," + PRODUCT_PRICE + " DOUBLE,"
+ PRODUCT_QUANTITY + " INTEGER" +");";
// CREATING USERLOGIN TABLE
private static final String CREATE_CONTACTS_USERLOGIN =
"CREATE TABLE " + TABLE_USERLOGIN_NEW + "("
+ USERLOGIN_ID + " INTEGER PRIMARY KEY," + USERLOGIN_USERCODE + " TEXT,"
+ USERLOGIN_NAME + " TEXT," + USERLOGIN_PASSWORD + " TEXT"+");";
// CREATING ORDERS TABLE
private static final String CREATE_CONTACTS_ORDERS =
"CREATE TABLE " + TABLE_ORDERS_NEW + "("
+ ORDER_ID + " INTEGER PRIMARY KEY,"
+ ORDER_ITEMS + " TEXT," + ORDER_DATE + " DATETIME"+");
public DatabaseHandler (Context context) throws IOException {
super(context,DB_NAME,null,1);
this.myContext=context;
boolean dbexist = checkDatabase();
if(dbexist)
{
//System.out.println("Database exists");
openDatabase();
}
else
{
System.out.println("Database doesn't exist");
createDatabase();
}
}
/**
* Creates a empty database on the system and rewrites it with your own database.
* */
public void createDatabase() throws IOException{
boolean dbexist = checkDatabase();
if(dbexist)
{
//System.out.println(" Database exists.");
}
else{
this.getReadableDatabase();
try{
copyDatabase();
}
catch(IOException e){
throw new Error("Error copying database");
}
}
}
/**
* Check if the database already exist to avoid re-copying the file each time you open the application.
* @return true if it exists, false if it doesn't
*/
public boolean checkDatabase() {
//SQLiteDatabase checkdb = null;
boolean checkdb = false;
try{
String myPath = DB_PATH + DB_NAME;
File dbfile = new File(myPath);
//checkdb = SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READWRITE);
checkdb = dbfile.exists();
}
catch(SQLiteException e){
System.out.println("Database doesn't exist");
}
return checkdb;
}
private void copyDatabase() throws IOException {
//Open your local db as the input stream
InputStream myinput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outfilename = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myoutput = new FileOutputStream("/data/data/com.WaqasAsgharBhalli.za_traders/databases/myDatabase.sqlite");
// transfer byte to inputfile to outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myinput.read(buffer))>0)
{
myoutput.write(buffer,0,length);
}
//Close the streams
myoutput.flush();
myoutput.close();
myinput.close();
}
public void openDatabase() throws SQLException
{
//Open the database
String mypath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(mypath, null, SQLiteDatabase.OPEN_READWRITE);
}
@Override
public synchronized void close(){
if(myDataBase != null){
myDataBase.close();
}
super.close();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS_NEW);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCT_NEW);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERLOGIN_NEW);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ORDERS_NEW);
// Create tables again
onCreate(db);
}
每当我尝试输入第二条记录时,我都会收到错误。
07-19 00:43:03.198: I/SQLiteAssetHelper(1741): successfully opened database myDatabase.sqlite
07-19 00:43:03.386: E/SQLiteDatabase(1741): Error inserting id=0 orderItems=[{"_product_name":"BETNESOL TAB","_product_Code":"0343","_price":21.6,"_id":6,"_quantity":29},{"_product_name":"Q.PIN TAB 100MG","_product_Code":"2132","_price":221.0,"_id":10,"_quantity":11}] orderDate=19-Jul-2014
07-19 00:43:03.386: E/SQLiteDatabase(1741): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
07-19 00:43:03.386: E/SQLiteDatabase(1741): at android.database.sqlite.SQLiteStatement.native_executeInsert(Native Method)
07-19 00:43:03.386: E/SQLiteDatabase(1741): at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:113)
07-19 00:43:03.386: E/SQLiteDatabase(1741): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1718)
07-19 00:43:03.386: E/SQLiteDatabase(1741): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1591)
07-19 00:43:03.386: E/SQLiteDatabase(1741): at com.WaqasAsgharBhalli.za_traders.DatabaseHandler.addOrders(DatabaseHandler.java:584)
07-19 00:43:03.386: E/SQLiteDatabase(1741): at com.WaqasAsgharBhalli.za_traders.SelectedMedicineList.saveOrders(SelectedMedicineList.java:139)
07-19 00:43:03.386: E/SQLiteDatabase(1741): at com.WaqasAsgharBhalli.za_traders.SelectedMedicineList.onOptionsItemSelected(SelectedMedicineList.java:117)
07-19 00:43:03.386: E/SQLiteDatabase(1741): at android.app.Activity.onMenuItemSelected(Activity.java:2502)
07-19 00:43:03.386: E/SQLiteDatabase(1741): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:950)
07-19 00:43:03.386: E/SQLiteDatabase(1741): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
07-19 00:43:03.386: E/SQLiteDatabase(1741): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
07-19 00:43:03.386: E/SQLiteDatabase(1741): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
07-19 00:43:03.386: E/SQLiteDatabase(1741): at com.android.internal.view.menu.ListMenuPresenter.onItemClick(ListMenuPresenter.java:163)
07-19 00:43:03.386: E/SQLiteDatabase(1741): at android.widget.AdapterView.performItemClick(AdapterView.java:292)
07-19 00:43:03.386: E/SQLiteDatabase(1741): at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
07-19 00:43:03.386: E/SQLiteDatabase(1741): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
07-19 00:43:03.386: E/SQLiteDatabase(1741): at android.widget.AbsListView$1.run(AbsListView.java:3168)
07-19 00:43:03.386: E/SQLiteDatabase(1741): at android.os.Handler.handleCallback(Handler.java:605)
07-19 00:43:03.386: E/SQLiteDatabase(1741): at android.os.Handler.dispatchMessage(Handler.java:92)
07-19 00:43:03.386: E/SQLiteDatabase(1741): at android.os.Looper.loop(Looper.java:137)
07-19 00:43:03.386: E/SQLiteDatabase(1741): at android.app.ActivityThread.main(ActivityThread.java:4340)
07-19 00:43:03.386: E/SQLiteDatabase(1741): at java.lang.reflect.Method.invokeNative(Native Method)
07-19 00:43:03.386: E/SQLiteDatabase(1741): at java.lang.reflect.Method.invoke(Method.java:511)
07-19 00:43:03.386: E/SQLiteDatabase(1741): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-19 00:43:03.386: E/SQLiteDatabase(1741): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-19 00:43:03.386: E/SQLiteDatabase(1741): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
您在ContentValues
传递给db.insert
时明确设置了ID,而不是让数据库自动为您生成ID。将ID设置为自动递增,除非您明确提供ID,否则数据库将生成ID,在这种情况下,它将尝试使用您指定的ID。
从代码中删除此行:
orderValues.put(ORDER_ID, ord.get_orderId());
答案 1 :(得分:0)
您有一个约束错误。这意味着您必须检查数据库的设置。
例如,如果您尝试使用相同的值插入主键两次,则会出现此错误。
您是否可以发布数据库帮助程序以查看您的输入是否符合数据库设置。
可以是唯一值,主键值等。
答案 2 :(得分:0)
如果您将id
设置为autoincrement
,则无法专门设置它(在这种情况下为ord.get_orderId()
)。
您应该删除orderValues.put(ORDER_ID, ord.get_orderId());
行。或者,在表格中创建另一个字段以存储ord.get_orderId()
。