我有2个值,imsi和电话号码。我将它们存储在database.i中获取getSIMContacts中的数据库问题functn.addContact将phonenum添加到数据库中。 getSIMContacts从数据库中检索phonenum。我的代码看起来像这样 - >
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;
}
读取帖子并编辑数据。 我做了更改但是当我检索数据时,我进入无限循环。代码如下
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是主键,具有自动递增和递减功能,并且还有另一个函数用于获取与键id值对应的移动数字。它看起来像这样但有一些问题。请帮我解决一下
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;
}
答案 0 :(得分:0)
您的查询错误,因为您在引号中指定了字段KEY_MO_NO
。
此
String selectQuery = "SELECT " + "KEY_MO_NO" +" FROM " + TABLE_NAME;
应该是
String selectQuery = "SELECT " + KEY_MO_NO +" FROM " + TABLE_NAME;