0
我有2個值,imsi和電話號碼。我將它們存儲在數據庫中.i getSIMContacts中的數據庫中出現問題functn.addContact將數據庫添加到phonenum中。 getSIMContacts從數據庫中檢索phonenum。我的代碼看起來像這個 - >檢索數據庫rawquery安卓不工作
public class PersonalContactsDatabase extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "PersonalContacts";
// Contacts table name
private static final String TABLE_NAME = "Contacts";
// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String IMEI_MOB = "imei";
private static final String KEY_MO_NO = "mobile_no";
Contact contact;
public PersonalContactsDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_NAME + "("
+ KEY_ID + " TEXT PRIMARY KEY,"
+ KEY_MO_NO + " TEXT,"
+ IMEI_MOB + " TEXT"
+ ")";
System.out.println("Create Query :::::"+CREATE_CONTACTS_TABLE);
db.execSQL(CREATE_CONTACTS_TABLE);
Log.e("DB Manager", "Db created Succesfully "+db);
//Toast.makeText(context, text, duration)
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
// Create tables again
onCreate(db);
}
/**
* All CRUD(Create, Read, Update, Delete) Operations
*/
// Adding new contact
void addContact(String contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
//values.put(IMEI_MOB, contact.getImei()); // Contact Name
values.put(KEY_MO_NO, contact); // Contact Phone
// Inserting Row
db.insert(TABLE_NAME, null, values);
db.close(); // Closing database connection
}
public List<String> getSIMContacts() {
List<String> contactList = new ArrayList<String>();
// Select All Query
String selectQuery = "SELECT " + KEY_MO_NO +" FROM " + TABLE_NAME;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
/*Contact contact = new Contact();
contact.setStrId(Integer.parseInt(cursor.getString(0)));
//contact.setName(cursor.getString(1));
contact.setMobileNum(cursor.getString(2));*/
// Adding contact to list
contactList.add(cursor.getString(0));
} while (cursor.moveToNext());
}
cursor.close();
// return contact list
return contactList;
}
IREAD崗位和編輯數據。 我做了更改,但當我檢索數據時,我陷入了無限循環。代碼是這個databasae如下
saveButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
List<String> list;
list=db.getSIMContacts();
while(list.iterator().hasNext())
{
String retrieved_num=list.iterator().next();
listItems.add(retrieved_num);
adapter.notifyDataSetChanged();
}
}
});
,我想KEY_ID能與自動增量和decremant功能主鍵,有用於獲取對應的密鑰ID value.it移動NUM另一個功能是這樣的但有一些問題。請幫我解決這個問題
String getContact(int id) {
String str="";
try{
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, new String[] { KEY_ID
}, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
str+=cursor.getString(2);
// return contact
cursor.close();
}catch(Exception e)
{
Log.e("Get Contact Exception", ""+e);
}
return str;
}