我正在使用android app将数据从json保存到sqlite数据库, 数据被保存但是如果用户再次登录将再次插入相同的数据在数据库的新行中
我要做的是停止为每次登录插入相同的数据 ,如果有新数据只保存
我尝试在addCustomer中添加此代码,但它只保存一行
Cursor cur = db.rawQuery("SELECT count(*) FROM " + BookingTabel.TABLE_CUSTOMER, null);
if (cur != null && cur.moveToFirst() && cur.getInt(0) > 0) {
Log.i(getClass().getName(), "table not empty");
}
else {
Log.i(getClass().getName(), "table is empty");
customer_id = db.insert(TABLE_CUSTOMER, null, values);
}
这是我的SqlitleHelper
public class BookingTabel extends SQLiteOpenHelper {
private static final int DB_VERSION = 1;
private static final String DB_NAME = "MyDB";
// Bookinks table name
private static final String TABLE_BOOKINDS = "bookings";
private static final String TABLE_CUSTOMER = "customer";
private static final String TABLE_SERVICE = "service";
private static final String TABLE_SERVICE_ATTRIBUTE = "service_attribute";
private static final String TABLE_SERVICE_ATTRIBUTE_VALUE = "service_attribute_value";
private static final String TABLE_INVOICE = "invoice";
private static final String TABLE_ESTIMATE = "estimate";
// Bookings Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_BOOKING_ID = "booking_id";
private static final String KEY_BOOKING_NUM = "booking_num";
private static final String KEY_TITLE = "title";
private static final String KEY_BOOKING_START= "booking_start";
private static final String KEY_BOOKING_END = "booking_end";
private static final String KEY_PROPERTY = "property_type";
private static final String KEY_DESCRIPTION = "description";
private static final String KEY_ACCEPT_STATUS = "accept_status";
private static final String KEY_BOOKING_ADDRESS = "booking_address";
private static final String KEY_PO_BOX = "po_box";
private static final String KEY_CREATED = "created";
private static final String KEY_POSTCODE = "postcode";
private static final String KEY_STATE = "state";
private static final String KEY_STREET_ADDRESS = "street_address";
private static final String KEY_STREET_NUMBER = "street_number";
private static final String KEY_SUBURB = "suburb";
private static final String KEY_UNIT_LOT_NUMBER = "unit_lot_number";
private static final String KEY_STATUS = "status";
private static final String KEY_CONVERT_STATUS = "convert_status";
private static final String KEY_QOUTE = "qoute";
private static final String KEY_SUB_TOTAL = "sub_total";
private static final String KEY_TOTAL_DISCOUNT = "total_discount";
private static final String KEY_BOOKING_DISTANCE = "booking_distance";
private static final String KEY_GST = "gst";
private static final String KEY_ORIGINAL_BOOKING_ID = "original_booking_id";
//*************** Customer Table Columns names *****************//
private static final String KEY_CUSTOMER_ID = "customer_id";
private static final String KEY_CUSTOMER_NAME = "name";
private static final String KEY_CUSTOMER_EMAIL1 = "email1";
private static final String KEY_CUSTOMER_EMAIL2 = "email2";
private static final String KEY_CUSTOMER_EMAIL3 = "email3";
private static final String KEY_CUSTOMER_MOBILE1 = "mobile1";
private static final String KEY_CUSTOMER_MOBILE2 = "mobile2";
private static final String KEY_CUSTOMER_MOBILE3 = "mobile3";
private static final String KEY_CUSTOMER_PHONE1 = "phone1";
private static final String KEY_CUSTOMER_PHONE2 = "phone2";
private static final String KEY_CUSTOMER_PHONE3 = "phone3";
private static final String KEY_CUSTOMER_CITY = "city";
private static final String KEY_CUSTOMER_COUNTRY = "country";
private static final String KEY_CUSTOMER_ADDRESS = "address";
private static final String KEY_CUSTOMER_FAX = "fax";
private static final String KEY_CUSTOMER_ORIGINAL_CUSTOMER_ID = "original_customer_id";
public BookingTabel(Context context) {
super(context, DB_NAME, null,DB_VERSION);
// TODO Auto-generated constructor stub
}
private static final String[] COLUMNS = {KEY_ID,KEY_BOOKING_NUM,KEY_TITLE,KEY_BOOKING_START,KEY_BOOKING_END,KEY_PROPERTY, KEY_CUSTOMER_ID,KEY_DESCRIPTION,KEY_ACCEPT_STATUS,KEY_BOOKING_ADDRESS,KEY_PO_BOX,KEY_CREATED , KEY_POSTCODE,KEY_STATE,KEY_STREET_ADDRESS,KEY_STREET_NUMBER,KEY_SUBURB,KEY_UNIT_LOT_NUMBER,KEY_STATUS, KEY_CONVERT_STATUS,KEY_QOUTE,KEY_SUB_TOTAL,KEY_TOTAL_DISCOUNT,KEY_BOOKING_DISTANCE,KEY_GST, KEY_ORIGINAL_BOOKING_ID};
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
// SQL statement to create book table
String CREATE_BOOKINDS_TABLE = "CREATE TABLE bookings ( " + "id INTEGER PRIMARY KEY ,"+ "booking_num VARCHAR(250) ,"+ "title VARCHAR(250) ," + "booking_start DATETIME ,"+"booking_end DATETIME ,"+"property_type VARCHAR(250) NOT NULL DEFAULT 'House'," + "customer_id LONG ,"+"description TEXT ,"+"accept_status VARCHAR(250) ,"+"booking_address VARCHAR(250), " + "po_box VARCHAR(250) , "+ "created INTEGER ,"+ "postcode INTEGER ,"+"state VARCHAR(250) ," + "street_address VARCHAR(250) ,"+"street_number INTEGER ,"+"suburb VARCHAR(250) ,"+"unit_lot_number INTEGER ," + "status VARCHAR(250) ,"+"convert_status VARCHAR(250) ,"+"qoute FLOAT ,"+"sub_total FLOAT ,"+"total_discount FLOAT ," + "booking_distance FLOAT ,"+"gst FLOAT ,"+"original_booking_id INTEGER )";
String CREATE_TABLE_CUSTOMER = "CREATE TABLE customer ( "+ "id INTEGER PRIMARY KEY,"+ "name VARCHAR(250),"+ "email1 VARCHAR(250)," + "email2 VARCHAR(250) ," + "email3 VARCHAR(250) , " + "mobile1 VARCHAR(250), " + "mobile2 VARCHAR(250), " + "mobile3 VARCHAR(250)," + "phone1 VARCHAR(250)," + "phone2 VARCHAR(250)," + "phone3 VARCHAR(250)," + "city VARCHAR(250)," + "country VARCHAR(250)," + "address VARCHAR(250)," + "fax VARCHAR9(250)," + "original_customer_id INTEGER )";
// create bookings table
db.execSQL(CREATE_BOOKINDS_TABLE);
db.execSQL(CREATE_TABLE_CUSTOMER);
}
@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS bookings");
// create fresh bookings table
this.onCreate(db);
}
public long addCustomer(String name,String email1,String email2, String email3, String mobile1,String mobile2,String mobile3,String phone1,String phone2,String phone3,String city,String country,String address ,String fax,String original_customer_id/*,String original_booking_id*/) {
// TODO Auto-generated method stub
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_CUSTOMER_NAME,name );
values.put(KEY_CUSTOMER_EMAIL1 ,email1 );
values.put(KEY_CUSTOMER_EMAIL2 ,email2 );
values.put(KEY_CUSTOMER_EMAIL3 ,email3 );
values.put(KEY_CUSTOMER_MOBILE1 ,mobile1);
values.put( KEY_CUSTOMER_MOBILE2,mobile2);
values.put( KEY_CUSTOMER_MOBILE3,mobile3);
values.put( KEY_CUSTOMER_PHONE1,phone1);
values.put( KEY_CUSTOMER_PHONE2,phone2 );
values.put( KEY_CUSTOMER_PHONE3,phone3 );
values.put( KEY_CUSTOMER_CITY,city);
values.put( KEY_CUSTOMER_COUNTRY,country );
values.put( KEY_CUSTOMER_ADDRESS,address );
values.put( KEY_CUSTOMER_FAX,fax);
values.put( KEY_CUSTOMER_ORIGINAL_CUSTOMER_ID,original_customer_id);
//values.put(KEY_ORIGINAL_BOOKING_ID, original_booking_id);
long customer_id = db.insert(TABLE_CUSTOMER, null, values);
db.close();
return customer_id ;
}
//*************** ADD Bookings In Database *****************//
public long addBookings(String booking_num,String title,String booking_start,String booking_end,String property_type,long customer_id,String description,String accept_status,String booking_address,String po_box,String created ,String postcode,String state,String street_address,String street_number,String suburb,String unit_lot_number,String status,String convert_status,String qoute,String sub_total,String total_discount,String booking_distance,String gst, String original_booking_id) {
// TODO Auto-generated method stub
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_BOOKING_NUM,booking_num );
values.put(KEY_TITLE,title );
values.put(KEY_BOOKING_START ,booking_start );
values.put(KEY_BOOKING_END ,booking_end );
values.put(KEY_PROPERTY ,property_type );
values.put(KEY_CUSTOMER_ID, customer_id);
// System.out.println("CUSTOMER ID:*****" + customer_id);
values.put( KEY_DESCRIPTION,description );
values.put( KEY_ACCEPT_STATUS,accept_status );
values.put( KEY_BOOKING_ADDRESS,booking_address );
values.put( KEY_PO_BOX,po_box );
values.put( KEY_CREATED,created );
values.put( KEY_POSTCODE,postcode);
values.put( KEY_STATE,state );
values.put( KEY_STREET_ADDRESS,street_address );
values.put( KEY_STREET_NUMBER,street_number);
values.put( KEY_SUBURB,suburb);
values.put( KEY_UNIT_LOT_NUMBER,unit_lot_number);
values.put( KEY_STATUS,status );
values.put( KEY_CONVERT_STATUS,convert_status);
values.put( KEY_QOUTE,qoute);
values.put( KEY_SUB_TOTAL,sub_total );
values.put( KEY_TOTAL_DISCOUNT,total_discount);
values.put( KEY_BOOKING_DISTANCE,booking_distance );
values.put( KEY_GST,gst);
values.put( KEY_ORIGINAL_BOOKING_ID,original_booking_id );
long booking_id = db.insert(TABLE_BOOKINDS, null, values);
db.close();
return booking_id;
}
答案 0 :(得分:0)
考虑在SQLite中使用INSERT OR IGNORE
:
customer
表中创建一个唯一字段,例如name VARCHAR(250) UNIQUE
execSQL
方法进行INSERT OR IGNORE
查询答案 1 :(得分:-1)
此代码将检查数据库中是否存在数据。现在,当用户登录时,您将不得不更改用于检查数据库中存储的任何唯一值的查询。
public static boolean CheckIsDataAlreadyInDBorNot(String TableName,
String dbfield, String fieldValue) {
SQLiteDatabase sqldb = EGLifeStyleApplication.sqLiteDatabase;
String Query = "Select * from " + TableName + " where " + dbfield + "="
+ fieldValue;
Cursor cursor = sqldb.rawQuery(Query, null);
if(cursor.getCount<=0){
return false;
}
return true;
}