首先,創建一個MySQLite Helper類
public class MySQLiteHelper extends SQLiteOpenHelper {
public static final String TABLE_CONTACTS = "contacts";
public static final String COLUMN_CONTACTS_ID = "_id";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_USERNAME ="username";
public static final String COLUMN_PASSWORD = "password"
private static final String DATABASE_NAME = "Contacts.db";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CONTACTS_CREATE = "create table "+ TABLE_CONTACTS+
"("+COLUMN_CONTACTS_ID+" integer primary key autoincrement, "
+COLUMN_NAME+" text not null, "
+COLUMN_USERNAME+" text not null,"
+COLUMN_PASSWORD+" text not null);";
public MySQLiteHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database){
database.execSQL(DATABASE_CONTACTS_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
Log.w(MySQLiteHelper.class.getName(),"Upgrading database from version "
+oldVersion
+ " to"
+newVersion
+", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS "+TABLE_CONTACTS);
onCreate(db);
}}
然後一個模型類
public class ContactModel {
private long id;
private String name;
private String username;
private String password;
///////////
public long getId(){
return id;
}
public void setId(long id){
this.id = id;
}
//////////
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public void setUsername(String username){
this.username = username;
}
////////////
public String getPassword(){
return password;
}
public void setPassword(String password){
this.password = password;
} }
然後一個數據源
public class ContactsDataSource {
private SQLiteDatabase database;
private MySQLiteHelper dbHelper;
private String[] allContactsColumns = {MySQLiteHelper.COLUMN_CONTACTS_ID,MySQLiteHelper.COLUMN_NAME,MySQLiteHelper.COLUMN_USERNAME,
MySQLiteHelper.COLUMN_PASSWORD};
public ContactsDataSource(Context context){
dbHelper = new MySQLiteHelper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close(){
dbHelper.close();
}
public ContactModel createContact(String name, String username, String password){
ContentValues values = new ContentValues();
values.put(MySQLiteHelper.COLUMN_NAME, name);
values.put(MySQLiteHelper.COLUMN_EMAIL,username);
values.put(MySQLiteHelper.COLUMN_PHONE,password);
long insertContactsId = database.insert(MySQLiteHelper.TABLE_CONTACTS, null, values);
Cursor cursor = database.query(MySQLiteHelper.TABLE_CONTACTS,allContactsColumns,MySQLiteHelper.COLUMN_CONTACTS_ID+" = "
+insertContactsId, null, null, null, null);
cursor.moveToFirst();
ContactModel newContact = cursorToContact(cursor);
cursor.close();
Log.d("written", "Info was written");
return newContact;
}
public void deleteContact(ContactModel contact){
long id = contact.getId();
System.out.println("Comment deleted with id: "+id);
database.delete(MySQLiteHelper.TABLE_CONTACTS,MySQLiteHelper.COLUMN_CONTACTS_ID
+" = "+id,null);
}
public ArrayList<ContactModel> getAllContacts(){
ArrayList<ContactModel> contacts = new ArrayList<>();
Cursor cursor = database.query(MySQLiteHelper.TABLE_CONTACTS, allContactsColumns, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()){
ContactModel contact = cursorToContact(cursor);
contacts.add(contact);
cursor.moveToNext();
}
cursor.close();
return contacts;
}
private ContactModel cursorToContact(Cursor cursor){
ContactModel contact = new ContactModel();
contact.setId(cursor.getLong(0));
contact.setName(cursor.getString(1));
contact.setUsername(cursor.getString(2));
contact.setPassword(cursor.getString(3));
return contact;
}}
您訪問數據源如下:
您可以通過調用ContactsDataSource.getAllContacts()來顯示列表視圖中的信息,並使用自定義listAdapter顯示結果。這個想法是,你將使用數據源的函數來獲取你想要的數據,並將它提供給模型類。從那裏你可以提取你需要的listview。讓我知道你是否需要更多的幫助。
您還提到想要在數據庫中創建多個表,這與使用上面的代碼一樣簡單,但只需添加另一個變量(如TABLE_CONTACTS2 =「contacts2」)並複製該表的函數即可。
您可以從這裏開始[Android CRUD教程](https://www.codeofaninja.com/2013/02/android-sqlite-tutorial。html) –
聽起來像一個艱難的。祝你好運! 你能不能僅僅爲我們提供一點作業?你已經嘗試了什麼?我們可以看到你的代碼,以便其他人可以修補它嗎? – mike510a
我需要關於如何在數據庫上設置表的幫助。表1用戶:id,姓名,用戶名,密碼,電子郵件字段。然後我有表2聯繫人:id,user_id,名字,姓氏,電話,電子郵件。我想知道如何匹配這兩個表,我認爲與user_id,但我不知道我是否在正確的軌道 –