如何从 sql 服务器向 sqlite 插入更新的记录
how to insert updated records in sqlite from sql server
在我的应用程序中,我从 sql 服务器获取记录并在单击按钮时存储在 sqlite 中,这工作正常,但我的问题是每次单击按钮时记录数都会加倍.例如如果 sql 服务器有 50 条记录,sqlite 在单击第一个按钮时获取 50 条记录,单击第二个按钮时记录为 100 条,依此类推。
我希望 sqlite 存储与 sql 服务器相同的记录数。我搜索了这个东西但不明白我在 android 中也是新的。如果我的问题不清楚,我们将不胜感激和抱歉。
这是我用来在 sqlite 中插入记录的代码。
public boolean insertUserData(String user_id, String user_pwd,String ff_code, String ff_name, String terr_code, String dg_code,String dist_code, String ff_mob,String ff_mgr, String imeino) {
Log.e(" inserting user details", "yes");
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(CONTACTS_COLUMN_USER_ID, user_id);
contentValues.put(CONTACTS_COLUMN_USER_PWD, user_pwd);
contentValues.put(CONTACTS_COLUMN_FF_CODE, ff_code);
contentValues.put(CONTACTS_COLUMN_FF_NAME, ff_name);
contentValues.put(CONTACTS_COLUMN_TERR_CODE, terr_code);
contentValues.put(CONTACTS_COLUMN_DG_CODE, dg_code);
contentValues.put(CONTACTS_COLUMN_DIST_CODE, dist_code);
contentValues.put(CONTACTS_COLUMN_FF_MOB, ff_mob);
contentValues.put(CONTACTS_COLUMN_ff_mgr, ff_mgr);
contentValues.put(CONTACTS_COLUMN_IMEI_NO, imeino);
db.insert("user_detail", null, contentValues);
return true;
}enter code here
您应该在 user_detail
table 定义中将字段定义为 PRIMARY KEY
以防止插入重复记录。看来user_id
是个不错的选择
如果服务器的数据可以更改,您必须在插入新数据之前删除所有旧数据。
如果您的行有一个未更改的 ID 声明为 PRIMARY KEY,您可以使用 replace 而不是 insert
。
一个解决方案是:
它将插入 N 次(通过单击按钮)
INSERT INTO book(id, name) VALUES(1001, 'Programming')
尝试重现:
INSERT OR REPLACE INTO book(id, name) VALUES(1001, 'Programming')
另一个解决方案是:
检查主键是否已经插入
public boolean CheckAcnoFound(int acno)
{
Cursor curchkacno;
curchkacno = mCon.DataFetch("select acno from acledger where acno="
+ acno );
if (curchkacno.getCount() != 0)
return true;
else
return false;
}
插入记录:
if (CheckAcnoFound((Integer.parseInt(xAcLedgerAcNo)))
{
Your Code
}
DataFetch 方法:
public Cursor DataFetch(String xQuery) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(xQuery, null);
return cursor;
}
在我的应用程序中,我从 sql 服务器获取记录并在单击按钮时存储在 sqlite 中,这工作正常,但我的问题是每次单击按钮时记录数都会加倍.例如如果 sql 服务器有 50 条记录,sqlite 在单击第一个按钮时获取 50 条记录,单击第二个按钮时记录为 100 条,依此类推。
我希望 sqlite 存储与 sql 服务器相同的记录数。我搜索了这个东西但不明白我在 android 中也是新的。如果我的问题不清楚,我们将不胜感激和抱歉。
这是我用来在 sqlite 中插入记录的代码。
public boolean insertUserData(String user_id, String user_pwd,String ff_code, String ff_name, String terr_code, String dg_code,String dist_code, String ff_mob,String ff_mgr, String imeino) {
Log.e(" inserting user details", "yes");
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(CONTACTS_COLUMN_USER_ID, user_id);
contentValues.put(CONTACTS_COLUMN_USER_PWD, user_pwd);
contentValues.put(CONTACTS_COLUMN_FF_CODE, ff_code);
contentValues.put(CONTACTS_COLUMN_FF_NAME, ff_name);
contentValues.put(CONTACTS_COLUMN_TERR_CODE, terr_code);
contentValues.put(CONTACTS_COLUMN_DG_CODE, dg_code);
contentValues.put(CONTACTS_COLUMN_DIST_CODE, dist_code);
contentValues.put(CONTACTS_COLUMN_FF_MOB, ff_mob);
contentValues.put(CONTACTS_COLUMN_ff_mgr, ff_mgr);
contentValues.put(CONTACTS_COLUMN_IMEI_NO, imeino);
db.insert("user_detail", null, contentValues);
return true;
}enter code here
您应该在 user_detail
table 定义中将字段定义为 PRIMARY KEY
以防止插入重复记录。看来user_id
是个不错的选择
如果服务器的数据可以更改,您必须在插入新数据之前删除所有旧数据。
如果您的行有一个未更改的 ID 声明为 PRIMARY KEY,您可以使用 replace 而不是 insert
。
一个解决方案是:
它将插入 N 次(通过单击按钮)
INSERT INTO book(id, name) VALUES(1001, 'Programming')
尝试重现:
INSERT OR REPLACE INTO book(id, name) VALUES(1001, 'Programming')
另一个解决方案是:
检查主键是否已经插入
public boolean CheckAcnoFound(int acno)
{
Cursor curchkacno;
curchkacno = mCon.DataFetch("select acno from acledger where acno="
+ acno );
if (curchkacno.getCount() != 0)
return true;
else
return false;
}
插入记录:
if (CheckAcnoFound((Integer.parseInt(xAcLedgerAcNo)))
{
Your Code
}
DataFetch 方法:
public Cursor DataFetch(String xQuery) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(xQuery, null);
return cursor;
}