【复习】使用 SQLiteDatabase 操作 SQLite 数据库
生活随笔
收集整理的這篇文章主要介紹了
【复习】使用 SQLiteDatabase 操作 SQLite 数据库
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Android提供了一個(gè)名為SQLiteDatabase的類,該類封裝了一些操作數(shù)據(jù)庫的API,使用該類可以完成對數(shù)據(jù)進(jìn)行添加(Create)、查詢(Retrieve)、更新(Update)和刪除(Delete)操作(這些操作簡稱為CRUD)。對SQLiteDatabase的學(xué)習(xí),我們應(yīng)該重點(diǎn)掌握execSQL()和rawQuery()方法。 execSQL()方法可以執(zhí)行insert、delete、update和CREATE TABLE之類有更改行為的SQL語句; rawQuery()方法用于執(zhí)行select語句。?
execSQL()方法的使用例子:?
SQLiteDatabase db = ....;?
db.execSQL("insert into person(name, age) values('測試數(shù)據(jù)', 4)");?
db.close();?
執(zhí)行上面SQL語句會(huì)往person表中添加進(jìn)一條記錄,在實(shí)際應(yīng)用中, 語句中的“測試數(shù)據(jù)”這些參數(shù)值會(huì)由用戶輸入界面提供,如果把用戶輸入的內(nèi)容原樣組拼到上面的insert語句, 當(dāng)用戶輸入的內(nèi)容含有單引號(hào)時(shí),組拼出來的SQL語句就會(huì)存在語法錯(cuò)誤。要解決這個(gè)問題需要對單引號(hào)進(jìn)行轉(zhuǎn)義,也就是把單引號(hào)轉(zhuǎn)換成兩個(gè)單引號(hào)。有些時(shí)候用戶往往還會(huì)輸入像“ & ”這些特殊SQL符號(hào),為保證組拼好的SQL語句語法正確,必須對SQL語句中的這些特殊SQL符號(hào)都進(jìn)行轉(zhuǎn)義,顯然,對每條SQL語句都做這樣的處理工作是比較煩瑣的。 SQLiteDatabase類提供了一個(gè)重載后的execSQL(String sql, Object[] bindArgs)方法,使用這個(gè)方法可以解決前面提到的問題,因?yàn)檫@個(gè)方法支持使用占位符參數(shù)(?)。使用例子如下:?
SQLiteDatabase db = ....;?
db.execSQL("insert into person(name, age) values(?,?)", new Object[]{"測試數(shù)據(jù)", 4}); ?
db.close();?
execSQL(String sql, Object[] bindArgs)方法的第一個(gè)參數(shù)為SQL語句,第二個(gè)參數(shù)為SQL語句中占位符參數(shù)的值,參數(shù)值在數(shù)組中的順序要和占位符的位置對應(yīng)。?
?
public class DatabaseHelper extends SQLiteOpenHelper { ?
? ? //類沒有實(shí)例化,是不能用作父類構(gòu)造器的參數(shù),必須聲明為靜態(tài) ?
? ? ? ? ?private static final String name = "itcast"; //數(shù)據(jù)庫名稱 ?
? ? ? ? ?private static final int version = 1; //數(shù)據(jù)庫版本 ?
? ? ? ? ?public DatabaseHelper(Context context) { ?
//第三個(gè)參數(shù)CursorFactory指定在執(zhí)行查詢時(shí)獲得一個(gè)游標(biāo)實(shí)例的工廠類,設(shè)置為null,代表使用系統(tǒng)默認(rèn)的工廠類 ?
? ? ? ? ? ? ? ? super(context, name, null, version); ?
? ? ? ? ?} ?
? ? ? ? @Override public void onCreate(SQLiteDatabase db) { ?
? ? ? ? ? ? ? db.execSQL("CREATE TABLE IF NOT EXISTS person (personid integer primary key autoincrement, name varchar(20), age INTEGER)"); ? ??
? ? ? ? ?} ?
? ? ? ? @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { ?
? ? ? ? ? ? ? ?db.execSQL(" ALTER TABLE person ADD phone VARCHAR(12) NULL "); //往表中增加一列 ?
? ? // DROP TABLE IF EXISTS person 刪除表 ?
? ? ? ?} ?
} ?
//在實(shí)際項(xiàng)目開發(fā)中,當(dāng)數(shù)據(jù)庫表結(jié)構(gòu)發(fā)生更新時(shí),應(yīng)該避免用戶存放于數(shù)據(jù)庫中的數(shù)據(jù)丟失。 ?
SQLiteDatabase的rawQuery() 用于執(zhí)行select語句,使用例子如下:
?SQLiteDatabase db = ....;?
Cursor cursor = db.rawQuery(“select * from person”, null);?
while (cursor.moveToNext()) {?
? ? int personid = cursor.getInt(0); //獲取第一列的值,第一列的索引從0開始?
? ? String name = cursor.getString(1);//獲取第二列的值?
? ? int age = cursor.getInt(2);//獲取第三列的值?
}?
cursor.close();?
db.close(); ?
rawQuery()方法的第一個(gè)參數(shù)為select語句;第二個(gè)參數(shù)為select語句中占位符參數(shù)的值,如果select語句沒有使用占位符,該參數(shù)可以設(shè)置為null。帶占位符參數(shù)的select語句使用例子如下:?
Cursor cursor = db.rawQuery("select * from person where name like ? and age=?", new String[]{"%傳智%", "4"});?
Cursor是結(jié)果集游標(biāo),用于對結(jié)果集進(jìn)行隨機(jī)訪問,如果大家熟悉jdbc, 其實(shí)Cursor與JDBC中的ResultSet作用很相似。使用moveToNext()方法可以將游標(biāo)從當(dāng)前行移動(dòng)到下一行,如果已經(jīng)移過了結(jié)果集的最后一行,返回結(jié)果為false,否則為true。另外Cursor 還有常用的moveToPrevious()方法(用于將游標(biāo)從當(dāng)前行移動(dòng)到上一行,如果已經(jīng)移過了結(jié)果集的第一行,返回值為false,否則為true )、moveToFirst()方法(用于將游標(biāo)移動(dòng)到結(jié)果集的第一行,如果結(jié)果集為空,返回值為false,否則為true )和moveToLast()方法(用于將游標(biāo)移動(dòng)到結(jié)果集的最后一行,如果結(jié)果集為空,返回值為false,否則為true )。?
除了前面給大家介紹的execSQL()和rawQuery()方法, SQLiteDatabase還專門提供了對應(yīng)于添加、刪除、更新、查詢的操作方法: insert()、delete()、update()和query() 。這些方法實(shí)際上是給那些不太了解SQL語法的菜鳥使用的,對于熟悉SQL語法的程序員而言,直接使用execSQL()和rawQuery()方法執(zhí)行SQL語句就能完成數(shù)據(jù)的添加、刪除、更新、查詢操作。?
Insert()方法用于添加數(shù)據(jù),各個(gè)字段的數(shù)據(jù)使用ContentValues進(jìn)行存放。 ContentValues類似于MAP,相對于MAP,它提供了存取數(shù)據(jù)對應(yīng)的put(String key, Xxx value)和getAsXxx(String key)方法, ?key為字段名稱,value為字段值,Xxx指的是各種常用的數(shù)據(jù)類型,如:String、Integer等。?
SQLiteDatabase db = databaseHelper.getWritableDatabase();?
ContentValues values = new ContentValues();?
values.put("name", "測試數(shù)據(jù)");?
values.put("age", 4);?
long rowid = db.insert(“person”, null, values);//返回新添記錄的行號(hào),與主鍵id無關(guān)?
不管第三個(gè)參數(shù)是否包含數(shù)據(jù),執(zhí)行Insert()方法必然會(huì)添加一條記錄,如果第三個(gè)參數(shù)為空,會(huì)添加一條除主鍵之外其他字段值為Null的記錄。Insert()方法內(nèi)部實(shí)際上通過構(gòu)造insert SQL語句完成數(shù)據(jù)的添加,Insert()方法的第二個(gè)參數(shù)用于指定空值字段的名稱,相信大家對該參數(shù)會(huì)感到疑惑,該參數(shù)的作用是什么?是這樣的:如果第三個(gè)參數(shù)values 為Null或者元素個(gè)數(shù)為0, 由于Insert()方法要求必須添加一條除了主鍵之外其它字段為Null值的記錄,為了滿足SQL語法的需要, insert語句必須給定一個(gè)字段名,如:insert into person(name) values(NULL),倘若不給定字段名 , insert語句就成了這樣: insert into person() values(),顯然這不滿足標(biāo)準(zhǔn)SQL的語法。對于字段名,建議使用主鍵之外的字段,如果使用了INTEGER類型的主鍵字段,執(zhí)行類似insert into person(personid) values(NULL)的insert語句后,該主鍵字段值也不會(huì)為NULL。如果第三個(gè)參數(shù)values 不為Null并且元素的個(gè)數(shù)大于0 ,可以把第二個(gè)參數(shù)設(shè)置為null。?
delete()方法的使用:?
SQLiteDatabase db = databaseHelper.getWritableDatabase();?
db.delete("person", "personid<?", new String[]{"2"});?
db.close();?
上面代碼用于從person表中刪除personid小于2的記錄。?
update()方法的使用:?
SQLiteDatabase db = databaseHelper.getWritableDatabase();?
ContentValues values = new ContentValues();?
values.put(“name”, “測試數(shù)據(jù)”);//key為字段名,value為值?
db.update("person", values, "personid=?", new String[]{"1"}); ?
db.close();?
上面代碼用于把person表中personid等于1的記錄的name字段的值改為“測試數(shù)據(jù)”。?
query()方法實(shí)際上是把select語句拆分成了若干個(gè)組成部分,然后作為方法的輸入?yún)?shù):?
SQLiteDatabase db = databaseHelper.getWritableDatabase();?
Cursor cursor = db.query("person", new String[]{"personid,name,age"}, "name like ?", new String[]{"%傳智%"}, null, null, "personid desc", "1,2");?
while (cursor.moveToNext()) {?
? ? ? ? ?int personid = cursor.getInt(0); //獲取第一列的值,第一列的索引從0開始?
? ? ? ? String name = cursor.getString(1);//獲取第二列的值?
? ? ? ? int age = cursor.getInt(2);//獲取第三列的值?
}?
cursor.close();?
db.close(); ?
上面代碼用于從person表中查找name字段含有“傳智”的記錄,匹配的記錄按personid降序排序,對排序后的結(jié)果略過第一條記錄,只獲取2條記錄。?
query(table, columns, selection, selectionArgs, groupBy, having, orderBy, limit)方法各參數(shù)的含義:?
table:表名。相當(dāng)于select語句from關(guān)鍵字后面的部分。如果是多表聯(lián)合查詢,可以用逗號(hào)將兩個(gè)表名分開。?
columns:要查詢出來的列名。相當(dāng)于select語句select關(guān)鍵字后面的部分。?
selection:查詢條件子句,相當(dāng)于select語句where關(guān)鍵字后面的部分,在條件子句允許使用占位符“?”?
selectionArgs:對應(yīng)于selection語句中占位符的值,值在數(shù)組中的位置與占位符在語句中的位置必須一致,否則就會(huì)有異常。?
groupBy:相當(dāng)于select語句group by關(guān)鍵字后面的部分?
having:相當(dāng)于select語句having關(guān)鍵字后面的部分?
orderBy:相當(dāng)于select語句order by關(guān)鍵字后面的部分,如:personid desc, age asc;?
limit:指定偏移量和獲取的記錄數(shù),相當(dāng)于select語句limit關(guān)鍵字后面的部分。?
package com.zyq.db; ?
import android.app.Activity; ?
import android.os.Bundle; ?
public class MainActivity extends Activity ??
{ ?
? ? @Override ?
? ? public void onCreate(Bundle savedInstanceState) ??
? ? { ?
? ? ? ? super.onCreate(savedInstanceState); ?
? ? ? ? setContentView(R.layout.main); ?
? ? } ?
} ?
package com.zyq.db; ?
import java.util.List; ?
import android.test.AndroidTestCase; ?
import android.util.Log; ?
import com.zyq.service.DBOpenHelper; ?
import com.zyq.service.PersonService; ?
import com.zyq.voo.Person; ?
??
/**?
?* 測試方法 通過Junit 單元測試?
?* 1.>實(shí)例化測試類?
?* 2.>把與應(yīng)用有關(guān)的上下文信息傳入到測試類實(shí)例?
?* 3.>運(yùn)行測試方法 ?
?* @author Administrator?
?*?
?*/ ?
public class PersonServiceTest extends AndroidTestCase ?
{ ?
? ? private final static String TAG="PersonServiceTest"; ?
? ? ??
? ? /**?
? ? ?* 測試創(chuàng)建數(shù)據(jù)庫?
? ? ?* @throws Throwable?
? ? ?*/ ?
? ? public void testCreateDB() throws Throwable ?
? ? { ?
? ? ? ? DBOpenHelper dbOpenHelper=new DBOpenHelper(this.getContext()); ?
? ? ? ? dbOpenHelper.getReadableDatabase(); //Create and/or open a database. ?
? ? } ?
? ? /**?
? ? ?* 測試新增一條記錄?
? ? ?* @throws Throwable?
? ? ?*/ ?
? ? public void testSave() throws Throwable ?
? ? { ?
? ? ? ? PersonService personService=new PersonService(this.getContext()); ?
? ? ? ? personService.save(new Person("zhangsan","1360215320")); ?
? ? ? ? personService.save(new Person("lisi","1123")); ?
? ? ? ? personService.save(new Person("lili","232")); ?
? ? ? ? personService.save(new Person("wangda","123123")); ?
? ? ? ? personService.save(new Person("laozhu","234532")); ?
? ? } ?
? ? /**?
? ? ?* 查找一條記錄?
? ? ?* @throws Throwable?
? ? ?*/ ?
? ? public void testFind() throws Throwable ?
? ? { ?
? ? ? ? PersonService personService=new PersonService(this.getContext()); ?
? ? ? ? Person person=personService.find(1); ?
? ? ? ? Log.i(TAG,person.toString()); ?
? ? } ?
? ? /**?
? ? ?* 測試更新一條記錄?
? ? ?* @throws Throwable?
? ? ?*/ ?
? ? public void testUpdate() throws Throwable ?
? ? { ?
? ? ? ? PersonService personService=new PersonService(this.getContext()); ?
? ? ? ? Person person=personService.find(1); ?
? ? ? ? person.setName("lisi"); ?
? ? ? ? personService.update(person); ?
? ? } ?
? ? /**?
? ? ?* 測試得到所有記錄數(shù)?
? ? ?* @throws Throwable?
? ? ?*/ ?
? ? public void testGetCount() throws Throwable ?
? ? { ?
? ? ? ? PersonService personService=new PersonService(this.getContext()); ?
? ? ? ? Log.i(TAG, personService.getCount()+"********"); ?
? ? } ?
? ? /**?
? ? ?* 測試分頁?
? ? ?* @throws Throwable?
? ? ?*/ ?
? ? public void testScroll() throws Throwable ?
? ? { ?
? ? ? ? PersonService personService=new PersonService(this.getContext()); ?
? ? ? ? List<Person> persons=personService.getScrollData(3, 3); ?
? ? ? ? for(Person person:persons) ?
? ? ? ? { ?
? ? ? ? ? ? Log.i(TAG, person.toString()); ?
? ? ? ? } ?
? ? } ?
? ? /**?
? ? ?* 測試刪除一條記錄?
? ? ?* @throws Throwable?
? ? ?*/ ?
? ? public void testDelete() throws Throwable ?
? ? { ?
? ? ? ? PersonService personService=new PersonService(this.getContext()); ?
? ? ? ? personService.delete(5); ?
? ? } ?
} ?
package com.zyq.service; ?
import android.content.Context; ?
import android.database.sqlite.SQLiteDatabase; ?
import android.database.sqlite.SQLiteOpenHelper; ?
public class DBOpenHelper extends SQLiteOpenHelper ?
{ ?
? ? /**?
? ? ?* 如果想額外的增加一個(gè)字段(需求)?
? ? ?* 可以把版本號(hào)更改掉 但必須 >=1?
? ? ?* 更改版本號(hào)之后 會(huì)根據(jù)版本號(hào)判斷是不是上次創(chuàng)建的時(shí)候 (目前的版本號(hào)和傳入的版本號(hào)是否一致 )?
? ? ?* 如果不是會(huì)執(zhí)行 onUpgrade() 方法?
? ? ?* @param context?
? ? ?*/ ?
? ? public DBOpenHelper(Context context) ?
? ? { ?
? ? ? ? super(context, "zyq.db", null, 2); ?
? ? } ?
? ? /**?
? ? ?* 在數(shù)據(jù)庫創(chuàng)建的時(shí)候第一個(gè)調(diào)用的方法?
? ? ?* 適合創(chuàng)建表結(jié)構(gòu)?
? ? ?*/ ?
? ? @Override ?
? ? public void onCreate(SQLiteDatabase db) ?
? ? { ?
? ? ? ? db.execSQL("CREATE TABLE person (personid integer primary key autoincrement, name varchar(20))");//創(chuàng)建表 ?
? ? } ?
? ? /**?
? ? ?* 更新表結(jié)構(gòu) 在數(shù)據(jù)庫版本號(hào)發(fā)生改變的時(shí)候調(diào)用?
? ? ?* 應(yīng)用升級(jí) ?
? ? ?*/ ?
? ? @Override ?
? ? public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) ?
? ? { ?
? ? ? ? db.execSQL("ALTER TABLE person ADD phone VARCHAR(12) NULL "); //往表中增加一列 ?
? ? } ?
} ?
<?xml version="1.0" encoding="utf-8"?> ?
<manifest xmlns:android="http://schemas.android.com/apk/res/android" ?
? ? ? package="com.zyq.db" ?
? ? ? android:versionCode="1" ?
? ? ? android:versionName="1.0"> ?
? ? <application android:icon="@drawable/icon" android:label="@string/app_name"> ?
? ? <uses-library android:name="android.test.runner" /> ?
? ? ? ? <activity android:name=".MainActivity" ?
? ? ? ? ? ? ? ? ? android:label="@string/app_name"> ?
? ? ? ? ? ? <intent-filter> ?
? ? ? ? ? ? ? ? <action android:name="android.intent.action.MAIN" /> ?
? ? ? ? ? ? ? ? <category android:name="android.intent.category.LAUNCHER" /> ?
? ? ? ? ? ? </intent-filter> ?
? ? ? ? </activity> ?
? ? </application> ?
? ? <uses-sdk android:minSdkVersion="8" /> ?
? ? <instrumentation android:name="android.test.InstrumentationTestRunner" ?
? ? ? ? android:targetPackage="com.zyq.db" android:label="Tests for My App" /> ?
</manifest> ??
package com.zyq.service; ?
import java.util.ArrayList; ?
import java.util.List; ?
import android.content.Context; ?
import android.database.Cursor; ?
import android.database.sqlite.SQLiteDatabase; ?
import com.zyq.voo.Person; ?
public class PersonService ?
{ ?
? ? private DBOpenHelper helper; ?
? ? public PersonService(Context context) ?
? ? { ?
? ? ? ? helper=new DBOpenHelper(context); ?
? ? } ?
? ? /**?
? ? ?* 新增一條記錄?
? ? ?* @param person?
? ? ?*/ ?
? ? public void save(Person person) ?
? ? { ?
? ? ? ? SQLiteDatabase db=helper.getWritableDatabase();//Create and/or open a database that will be used for reading and writing ?
? ? ? ? db.execSQL("INSERT INTO person(name,phone) values(?,?)",new Object[]{person.getName().trim(),person.getPhone().trim()});//使用占位符進(jìn)行轉(zhuǎn)譯 ?
// ? ? ?db.close(); ?不關(guān)數(shù)據(jù)庫連接 。可以提高性能 因?yàn)閯?chuàng)建數(shù)據(jù)庫的時(shí)候的操作模式是私有的。 ?
// ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?代表此數(shù)據(jù)庫,只能被本應(yīng)用所訪問 單用戶的,可以維持長久的鏈接 ?
? ? } ??
? ? /**?
? ? ?* 更新某一條記錄?
? ? ?* @param person?
? ? ?*/ ?
? ? public void update(Person person) ?
? ? { ?
? ? ? ? SQLiteDatabase db=helper.getWritableDatabase(); ?
? ? ? ? db.execSQL("update person set phone=?,name=? where personid=?", ?
? ? ? ? ? ? ? ? ? ? new Object[]{person.getPhone().trim(),person.getName().trim(),person.getId()}); ?
? ? } ?
? ? /**?
? ? ?* 根據(jù)ID查詢某條記錄?
? ? ?* @param id?
? ? ?* @return?
? ? ?*/ ?
? ? public Person find(Integer id) ?
? ? { ?
? ? ? ? SQLiteDatabase db=helper.getReadableDatabase(); ?
? ? ? ? Cursor cursor=db.rawQuery("select * from person where personid=?", new String[]{id.toString()});//Cursor 游標(biāo)和 ResultSet 很像 ?
? ? ? ? if(cursor.moveToFirst())//Move the cursor to the first row. This method will return false if the cursor is empty. ?
? ? ? ? { ?
? ? ? ? ? ? int personid=cursor.getInt(cursor.getColumnIndex("personid")); ?
? ? ? ? ? ? String name=cursor.getString(cursor.getColumnIndex("name")); ?
? ? ? ? ? ? String phone=cursor.getString(cursor.getColumnIndex("phone")); ?
? ? ? ? ? ? ??
? ? ? ? ? ? return new Person(personid,name,phone); ?
? ? ? ? } ?
? ? ? ? return null; ?
? ? } ?
? ? /**?
? ? ?* 刪除某一條記錄?
? ? ?* @param id?
? ? ?*/ ?
? ? public void delete(Integer id) ?
? ? { ?
? ? ? ? SQLiteDatabase db=helper.getWritableDatabase(); ?
? ? ? ? db.execSQL("delete from person where personid=?", ?
? ? ? ? ? ? ? ? ? ? new Object[]{id}); ?
? ? } ?
? ? ??
? ? /**?
? ? ?* 得到記錄數(shù)?
? ? ?* @return?
? ? ?*/ ?
? ? public long getCount() ?
? ? { ?
? ? ? ? SQLiteDatabase db=helper.getReadableDatabase(); ?
? ? ? ? Cursor cursor=db.rawQuery("select count(*) from person", null); ?
? ? ? ? cursor.moveToFirst(); ?
? ? ? ? return cursor.getLong(0); ?
? ? } ?
? ? /**?
? ? ?* 分頁查詢方法 SQL語句跟MySQL的語法一樣?
? ? ?* @return?
? ? ?*/ ?
? ? public List<Person> getScrollData(int offset,int maxResult) ?
? ? { ?
? ? ? ? List<Person> persons=new ArrayList<Person>(); ?
? ? ? ? SQLiteDatabase db=helper.getReadableDatabase(); ?
? ? ? ? Cursor cursor=db.rawQuery("select * from person limit ?,?", ??
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? new String[]{String.valueOf(offset),String.valueOf(maxResult)}); ?
? ? ? ? while (cursor.moveToNext()) ?
? ? ? ? { ?
? ? ? ? ? ? int personid=cursor.getInt(cursor.getColumnIndex("personid")); ?
? ? ? ? ? ? String name=cursor.getString(cursor.getColumnIndex("name")); ?
? ? ? ? ? ? String phone=cursor.getString(cursor.getColumnIndex("phone")); ?
? ? ? ? ? ? ??
? ? ? ? ? ? persons.add(new Person(personid,name,phone)); ?
? ? ? ? } ?
? ? ? ? ??
? ? ? ? return persons; ?
? ? } ?
} ?
package com.zyq.voo; ?
public class Person ?
{ ?
? ? private Integer id; ?
? ? private String name; ?
? ? private String phone; ?
? ? ??
? ? public Person(int personid, String name, String phone) ?
? ? { ?
? ? ? ? this.id=personid; ?
? ? ? ? this.name=name; ?
? ? ? ? this.phone=phone; ?
? ? } ?
? ? ??
? ? public Person(String name, String phone) ?
? ? { ?
? ? ? ? this.name = name; ?
? ? ? ? this.phone = phone; ?
? ? } ?
? ? public String toString() ?
? ? { ?
? ? ? ? return "Person [id=" + id + ", name=" + name + ", phone=" + phone + "]"; ?
? ? } ?
? ? public Integer getId() ?
? ? { ?
? ? ? ? return id; ?
? ? } ?
? ? public void setId(Integer id) ?
? ? { ?
? ? ? ? this.id = id; ?
? ? } ?
? ? public String getName() ?
? ? { ?
? ? ? ? return name; ?
? ? } ?
? ? public void setName(String name) ?
? ? { ?
? ? ? ? this.name = name; ?
? ? } ?
? ? public String getPhone() ?
? ? { ?
? ? ? ? return phone; ?
? ? } ?
? ? public void setPhone(String phone) ?
? ? { ?
? ? ? ? this.phone = phone; ?
? ? } ?
? ? ??
? ? ??
? ? ??
} ? 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)
execSQL()方法的使用例子:?
SQLiteDatabase db = ....;?
db.execSQL("insert into person(name, age) values('測試數(shù)據(jù)', 4)");?
db.close();?
執(zhí)行上面SQL語句會(huì)往person表中添加進(jìn)一條記錄,在實(shí)際應(yīng)用中, 語句中的“測試數(shù)據(jù)”這些參數(shù)值會(huì)由用戶輸入界面提供,如果把用戶輸入的內(nèi)容原樣組拼到上面的insert語句, 當(dāng)用戶輸入的內(nèi)容含有單引號(hào)時(shí),組拼出來的SQL語句就會(huì)存在語法錯(cuò)誤。要解決這個(gè)問題需要對單引號(hào)進(jìn)行轉(zhuǎn)義,也就是把單引號(hào)轉(zhuǎn)換成兩個(gè)單引號(hào)。有些時(shí)候用戶往往還會(huì)輸入像“ & ”這些特殊SQL符號(hào),為保證組拼好的SQL語句語法正確,必須對SQL語句中的這些特殊SQL符號(hào)都進(jìn)行轉(zhuǎn)義,顯然,對每條SQL語句都做這樣的處理工作是比較煩瑣的。 SQLiteDatabase類提供了一個(gè)重載后的execSQL(String sql, Object[] bindArgs)方法,使用這個(gè)方法可以解決前面提到的問題,因?yàn)檫@個(gè)方法支持使用占位符參數(shù)(?)。使用例子如下:?
SQLiteDatabase db = ....;?
db.execSQL("insert into person(name, age) values(?,?)", new Object[]{"測試數(shù)據(jù)", 4}); ?
db.close();?
execSQL(String sql, Object[] bindArgs)方法的第一個(gè)參數(shù)為SQL語句,第二個(gè)參數(shù)為SQL語句中占位符參數(shù)的值,參數(shù)值在數(shù)組中的順序要和占位符的位置對應(yīng)。?
?
public class DatabaseHelper extends SQLiteOpenHelper { ?
? ? //類沒有實(shí)例化,是不能用作父類構(gòu)造器的參數(shù),必須聲明為靜態(tài) ?
? ? ? ? ?private static final String name = "itcast"; //數(shù)據(jù)庫名稱 ?
? ? ? ? ?private static final int version = 1; //數(shù)據(jù)庫版本 ?
? ? ? ? ?public DatabaseHelper(Context context) { ?
//第三個(gè)參數(shù)CursorFactory指定在執(zhí)行查詢時(shí)獲得一個(gè)游標(biāo)實(shí)例的工廠類,設(shè)置為null,代表使用系統(tǒng)默認(rèn)的工廠類 ?
? ? ? ? ? ? ? ? super(context, name, null, version); ?
? ? ? ? ?} ?
? ? ? ? @Override public void onCreate(SQLiteDatabase db) { ?
? ? ? ? ? ? ? db.execSQL("CREATE TABLE IF NOT EXISTS person (personid integer primary key autoincrement, name varchar(20), age INTEGER)"); ? ??
? ? ? ? ?} ?
? ? ? ? @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { ?
? ? ? ? ? ? ? ?db.execSQL(" ALTER TABLE person ADD phone VARCHAR(12) NULL "); //往表中增加一列 ?
? ? // DROP TABLE IF EXISTS person 刪除表 ?
? ? ? ?} ?
} ?
//在實(shí)際項(xiàng)目開發(fā)中,當(dāng)數(shù)據(jù)庫表結(jié)構(gòu)發(fā)生更新時(shí),應(yīng)該避免用戶存放于數(shù)據(jù)庫中的數(shù)據(jù)丟失。 ?
SQLiteDatabase的rawQuery() 用于執(zhí)行select語句,使用例子如下:
?SQLiteDatabase db = ....;?
Cursor cursor = db.rawQuery(“select * from person”, null);?
while (cursor.moveToNext()) {?
? ? int personid = cursor.getInt(0); //獲取第一列的值,第一列的索引從0開始?
? ? String name = cursor.getString(1);//獲取第二列的值?
? ? int age = cursor.getInt(2);//獲取第三列的值?
}?
cursor.close();?
db.close(); ?
rawQuery()方法的第一個(gè)參數(shù)為select語句;第二個(gè)參數(shù)為select語句中占位符參數(shù)的值,如果select語句沒有使用占位符,該參數(shù)可以設(shè)置為null。帶占位符參數(shù)的select語句使用例子如下:?
Cursor cursor = db.rawQuery("select * from person where name like ? and age=?", new String[]{"%傳智%", "4"});?
Cursor是結(jié)果集游標(biāo),用于對結(jié)果集進(jìn)行隨機(jī)訪問,如果大家熟悉jdbc, 其實(shí)Cursor與JDBC中的ResultSet作用很相似。使用moveToNext()方法可以將游標(biāo)從當(dāng)前行移動(dòng)到下一行,如果已經(jīng)移過了結(jié)果集的最后一行,返回結(jié)果為false,否則為true。另外Cursor 還有常用的moveToPrevious()方法(用于將游標(biāo)從當(dāng)前行移動(dòng)到上一行,如果已經(jīng)移過了結(jié)果集的第一行,返回值為false,否則為true )、moveToFirst()方法(用于將游標(biāo)移動(dòng)到結(jié)果集的第一行,如果結(jié)果集為空,返回值為false,否則為true )和moveToLast()方法(用于將游標(biāo)移動(dòng)到結(jié)果集的最后一行,如果結(jié)果集為空,返回值為false,否則為true )。?
除了前面給大家介紹的execSQL()和rawQuery()方法, SQLiteDatabase還專門提供了對應(yīng)于添加、刪除、更新、查詢的操作方法: insert()、delete()、update()和query() 。這些方法實(shí)際上是給那些不太了解SQL語法的菜鳥使用的,對于熟悉SQL語法的程序員而言,直接使用execSQL()和rawQuery()方法執(zhí)行SQL語句就能完成數(shù)據(jù)的添加、刪除、更新、查詢操作。?
Insert()方法用于添加數(shù)據(jù),各個(gè)字段的數(shù)據(jù)使用ContentValues進(jìn)行存放。 ContentValues類似于MAP,相對于MAP,它提供了存取數(shù)據(jù)對應(yīng)的put(String key, Xxx value)和getAsXxx(String key)方法, ?key為字段名稱,value為字段值,Xxx指的是各種常用的數(shù)據(jù)類型,如:String、Integer等。?
SQLiteDatabase db = databaseHelper.getWritableDatabase();?
ContentValues values = new ContentValues();?
values.put("name", "測試數(shù)據(jù)");?
values.put("age", 4);?
long rowid = db.insert(“person”, null, values);//返回新添記錄的行號(hào),與主鍵id無關(guān)?
不管第三個(gè)參數(shù)是否包含數(shù)據(jù),執(zhí)行Insert()方法必然會(huì)添加一條記錄,如果第三個(gè)參數(shù)為空,會(huì)添加一條除主鍵之外其他字段值為Null的記錄。Insert()方法內(nèi)部實(shí)際上通過構(gòu)造insert SQL語句完成數(shù)據(jù)的添加,Insert()方法的第二個(gè)參數(shù)用于指定空值字段的名稱,相信大家對該參數(shù)會(huì)感到疑惑,該參數(shù)的作用是什么?是這樣的:如果第三個(gè)參數(shù)values 為Null或者元素個(gè)數(shù)為0, 由于Insert()方法要求必須添加一條除了主鍵之外其它字段為Null值的記錄,為了滿足SQL語法的需要, insert語句必須給定一個(gè)字段名,如:insert into person(name) values(NULL),倘若不給定字段名 , insert語句就成了這樣: insert into person() values(),顯然這不滿足標(biāo)準(zhǔn)SQL的語法。對于字段名,建議使用主鍵之外的字段,如果使用了INTEGER類型的主鍵字段,執(zhí)行類似insert into person(personid) values(NULL)的insert語句后,該主鍵字段值也不會(huì)為NULL。如果第三個(gè)參數(shù)values 不為Null并且元素的個(gè)數(shù)大于0 ,可以把第二個(gè)參數(shù)設(shè)置為null。?
delete()方法的使用:?
SQLiteDatabase db = databaseHelper.getWritableDatabase();?
db.delete("person", "personid<?", new String[]{"2"});?
db.close();?
上面代碼用于從person表中刪除personid小于2的記錄。?
update()方法的使用:?
SQLiteDatabase db = databaseHelper.getWritableDatabase();?
ContentValues values = new ContentValues();?
values.put(“name”, “測試數(shù)據(jù)”);//key為字段名,value為值?
db.update("person", values, "personid=?", new String[]{"1"}); ?
db.close();?
上面代碼用于把person表中personid等于1的記錄的name字段的值改為“測試數(shù)據(jù)”。?
query()方法實(shí)際上是把select語句拆分成了若干個(gè)組成部分,然后作為方法的輸入?yún)?shù):?
SQLiteDatabase db = databaseHelper.getWritableDatabase();?
Cursor cursor = db.query("person", new String[]{"personid,name,age"}, "name like ?", new String[]{"%傳智%"}, null, null, "personid desc", "1,2");?
while (cursor.moveToNext()) {?
? ? ? ? ?int personid = cursor.getInt(0); //獲取第一列的值,第一列的索引從0開始?
? ? ? ? String name = cursor.getString(1);//獲取第二列的值?
? ? ? ? int age = cursor.getInt(2);//獲取第三列的值?
}?
cursor.close();?
db.close(); ?
上面代碼用于從person表中查找name字段含有“傳智”的記錄,匹配的記錄按personid降序排序,對排序后的結(jié)果略過第一條記錄,只獲取2條記錄。?
query(table, columns, selection, selectionArgs, groupBy, having, orderBy, limit)方法各參數(shù)的含義:?
table:表名。相當(dāng)于select語句from關(guān)鍵字后面的部分。如果是多表聯(lián)合查詢,可以用逗號(hào)將兩個(gè)表名分開。?
columns:要查詢出來的列名。相當(dāng)于select語句select關(guān)鍵字后面的部分。?
selection:查詢條件子句,相當(dāng)于select語句where關(guān)鍵字后面的部分,在條件子句允許使用占位符“?”?
selectionArgs:對應(yīng)于selection語句中占位符的值,值在數(shù)組中的位置與占位符在語句中的位置必須一致,否則就會(huì)有異常。?
groupBy:相當(dāng)于select語句group by關(guān)鍵字后面的部分?
having:相當(dāng)于select語句having關(guān)鍵字后面的部分?
orderBy:相當(dāng)于select語句order by關(guān)鍵字后面的部分,如:personid desc, age asc;?
limit:指定偏移量和獲取的記錄數(shù),相當(dāng)于select語句limit關(guān)鍵字后面的部分。?
package com.zyq.db; ?
import android.app.Activity; ?
import android.os.Bundle; ?
public class MainActivity extends Activity ??
{ ?
? ? @Override ?
? ? public void onCreate(Bundle savedInstanceState) ??
? ? { ?
? ? ? ? super.onCreate(savedInstanceState); ?
? ? ? ? setContentView(R.layout.main); ?
? ? } ?
} ?
package com.zyq.db; ?
import java.util.List; ?
import android.test.AndroidTestCase; ?
import android.util.Log; ?
import com.zyq.service.DBOpenHelper; ?
import com.zyq.service.PersonService; ?
import com.zyq.voo.Person; ?
??
/**?
?* 測試方法 通過Junit 單元測試?
?* 1.>實(shí)例化測試類?
?* 2.>把與應(yīng)用有關(guān)的上下文信息傳入到測試類實(shí)例?
?* 3.>運(yùn)行測試方法 ?
?* @author Administrator?
?*?
?*/ ?
public class PersonServiceTest extends AndroidTestCase ?
{ ?
? ? private final static String TAG="PersonServiceTest"; ?
? ? ??
? ? /**?
? ? ?* 測試創(chuàng)建數(shù)據(jù)庫?
? ? ?* @throws Throwable?
? ? ?*/ ?
? ? public void testCreateDB() throws Throwable ?
? ? { ?
? ? ? ? DBOpenHelper dbOpenHelper=new DBOpenHelper(this.getContext()); ?
? ? ? ? dbOpenHelper.getReadableDatabase(); //Create and/or open a database. ?
? ? } ?
? ? /**?
? ? ?* 測試新增一條記錄?
? ? ?* @throws Throwable?
? ? ?*/ ?
? ? public void testSave() throws Throwable ?
? ? { ?
? ? ? ? PersonService personService=new PersonService(this.getContext()); ?
? ? ? ? personService.save(new Person("zhangsan","1360215320")); ?
? ? ? ? personService.save(new Person("lisi","1123")); ?
? ? ? ? personService.save(new Person("lili","232")); ?
? ? ? ? personService.save(new Person("wangda","123123")); ?
? ? ? ? personService.save(new Person("laozhu","234532")); ?
? ? } ?
? ? /**?
? ? ?* 查找一條記錄?
? ? ?* @throws Throwable?
? ? ?*/ ?
? ? public void testFind() throws Throwable ?
? ? { ?
? ? ? ? PersonService personService=new PersonService(this.getContext()); ?
? ? ? ? Person person=personService.find(1); ?
? ? ? ? Log.i(TAG,person.toString()); ?
? ? } ?
? ? /**?
? ? ?* 測試更新一條記錄?
? ? ?* @throws Throwable?
? ? ?*/ ?
? ? public void testUpdate() throws Throwable ?
? ? { ?
? ? ? ? PersonService personService=new PersonService(this.getContext()); ?
? ? ? ? Person person=personService.find(1); ?
? ? ? ? person.setName("lisi"); ?
? ? ? ? personService.update(person); ?
? ? } ?
? ? /**?
? ? ?* 測試得到所有記錄數(shù)?
? ? ?* @throws Throwable?
? ? ?*/ ?
? ? public void testGetCount() throws Throwable ?
? ? { ?
? ? ? ? PersonService personService=new PersonService(this.getContext()); ?
? ? ? ? Log.i(TAG, personService.getCount()+"********"); ?
? ? } ?
? ? /**?
? ? ?* 測試分頁?
? ? ?* @throws Throwable?
? ? ?*/ ?
? ? public void testScroll() throws Throwable ?
? ? { ?
? ? ? ? PersonService personService=new PersonService(this.getContext()); ?
? ? ? ? List<Person> persons=personService.getScrollData(3, 3); ?
? ? ? ? for(Person person:persons) ?
? ? ? ? { ?
? ? ? ? ? ? Log.i(TAG, person.toString()); ?
? ? ? ? } ?
? ? } ?
? ? /**?
? ? ?* 測試刪除一條記錄?
? ? ?* @throws Throwable?
? ? ?*/ ?
? ? public void testDelete() throws Throwable ?
? ? { ?
? ? ? ? PersonService personService=new PersonService(this.getContext()); ?
? ? ? ? personService.delete(5); ?
? ? } ?
} ?
package com.zyq.service; ?
import android.content.Context; ?
import android.database.sqlite.SQLiteDatabase; ?
import android.database.sqlite.SQLiteOpenHelper; ?
public class DBOpenHelper extends SQLiteOpenHelper ?
{ ?
? ? /**?
? ? ?* 如果想額外的增加一個(gè)字段(需求)?
? ? ?* 可以把版本號(hào)更改掉 但必須 >=1?
? ? ?* 更改版本號(hào)之后 會(huì)根據(jù)版本號(hào)判斷是不是上次創(chuàng)建的時(shí)候 (目前的版本號(hào)和傳入的版本號(hào)是否一致 )?
? ? ?* 如果不是會(huì)執(zhí)行 onUpgrade() 方法?
? ? ?* @param context?
? ? ?*/ ?
? ? public DBOpenHelper(Context context) ?
? ? { ?
? ? ? ? super(context, "zyq.db", null, 2); ?
? ? } ?
? ? /**?
? ? ?* 在數(shù)據(jù)庫創(chuàng)建的時(shí)候第一個(gè)調(diào)用的方法?
? ? ?* 適合創(chuàng)建表結(jié)構(gòu)?
? ? ?*/ ?
? ? @Override ?
? ? public void onCreate(SQLiteDatabase db) ?
? ? { ?
? ? ? ? db.execSQL("CREATE TABLE person (personid integer primary key autoincrement, name varchar(20))");//創(chuàng)建表 ?
? ? } ?
? ? /**?
? ? ?* 更新表結(jié)構(gòu) 在數(shù)據(jù)庫版本號(hào)發(fā)生改變的時(shí)候調(diào)用?
? ? ?* 應(yīng)用升級(jí) ?
? ? ?*/ ?
? ? @Override ?
? ? public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) ?
? ? { ?
? ? ? ? db.execSQL("ALTER TABLE person ADD phone VARCHAR(12) NULL "); //往表中增加一列 ?
? ? } ?
} ?
<?xml version="1.0" encoding="utf-8"?> ?
<manifest xmlns:android="http://schemas.android.com/apk/res/android" ?
? ? ? package="com.zyq.db" ?
? ? ? android:versionCode="1" ?
? ? ? android:versionName="1.0"> ?
? ? <application android:icon="@drawable/icon" android:label="@string/app_name"> ?
? ? <uses-library android:name="android.test.runner" /> ?
? ? ? ? <activity android:name=".MainActivity" ?
? ? ? ? ? ? ? ? ? android:label="@string/app_name"> ?
? ? ? ? ? ? <intent-filter> ?
? ? ? ? ? ? ? ? <action android:name="android.intent.action.MAIN" /> ?
? ? ? ? ? ? ? ? <category android:name="android.intent.category.LAUNCHER" /> ?
? ? ? ? ? ? </intent-filter> ?
? ? ? ? </activity> ?
? ? </application> ?
? ? <uses-sdk android:minSdkVersion="8" /> ?
? ? <instrumentation android:name="android.test.InstrumentationTestRunner" ?
? ? ? ? android:targetPackage="com.zyq.db" android:label="Tests for My App" /> ?
</manifest> ??
package com.zyq.service; ?
import java.util.ArrayList; ?
import java.util.List; ?
import android.content.Context; ?
import android.database.Cursor; ?
import android.database.sqlite.SQLiteDatabase; ?
import com.zyq.voo.Person; ?
public class PersonService ?
{ ?
? ? private DBOpenHelper helper; ?
? ? public PersonService(Context context) ?
? ? { ?
? ? ? ? helper=new DBOpenHelper(context); ?
? ? } ?
? ? /**?
? ? ?* 新增一條記錄?
? ? ?* @param person?
? ? ?*/ ?
? ? public void save(Person person) ?
? ? { ?
? ? ? ? SQLiteDatabase db=helper.getWritableDatabase();//Create and/or open a database that will be used for reading and writing ?
? ? ? ? db.execSQL("INSERT INTO person(name,phone) values(?,?)",new Object[]{person.getName().trim(),person.getPhone().trim()});//使用占位符進(jìn)行轉(zhuǎn)譯 ?
// ? ? ?db.close(); ?不關(guān)數(shù)據(jù)庫連接 。可以提高性能 因?yàn)閯?chuàng)建數(shù)據(jù)庫的時(shí)候的操作模式是私有的。 ?
// ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?代表此數(shù)據(jù)庫,只能被本應(yīng)用所訪問 單用戶的,可以維持長久的鏈接 ?
? ? } ??
? ? /**?
? ? ?* 更新某一條記錄?
? ? ?* @param person?
? ? ?*/ ?
? ? public void update(Person person) ?
? ? { ?
? ? ? ? SQLiteDatabase db=helper.getWritableDatabase(); ?
? ? ? ? db.execSQL("update person set phone=?,name=? where personid=?", ?
? ? ? ? ? ? ? ? ? ? new Object[]{person.getPhone().trim(),person.getName().trim(),person.getId()}); ?
? ? } ?
? ? /**?
? ? ?* 根據(jù)ID查詢某條記錄?
? ? ?* @param id?
? ? ?* @return?
? ? ?*/ ?
? ? public Person find(Integer id) ?
? ? { ?
? ? ? ? SQLiteDatabase db=helper.getReadableDatabase(); ?
? ? ? ? Cursor cursor=db.rawQuery("select * from person where personid=?", new String[]{id.toString()});//Cursor 游標(biāo)和 ResultSet 很像 ?
? ? ? ? if(cursor.moveToFirst())//Move the cursor to the first row. This method will return false if the cursor is empty. ?
? ? ? ? { ?
? ? ? ? ? ? int personid=cursor.getInt(cursor.getColumnIndex("personid")); ?
? ? ? ? ? ? String name=cursor.getString(cursor.getColumnIndex("name")); ?
? ? ? ? ? ? String phone=cursor.getString(cursor.getColumnIndex("phone")); ?
? ? ? ? ? ? ??
? ? ? ? ? ? return new Person(personid,name,phone); ?
? ? ? ? } ?
? ? ? ? return null; ?
? ? } ?
? ? /**?
? ? ?* 刪除某一條記錄?
? ? ?* @param id?
? ? ?*/ ?
? ? public void delete(Integer id) ?
? ? { ?
? ? ? ? SQLiteDatabase db=helper.getWritableDatabase(); ?
? ? ? ? db.execSQL("delete from person where personid=?", ?
? ? ? ? ? ? ? ? ? ? new Object[]{id}); ?
? ? } ?
? ? ??
? ? /**?
? ? ?* 得到記錄數(shù)?
? ? ?* @return?
? ? ?*/ ?
? ? public long getCount() ?
? ? { ?
? ? ? ? SQLiteDatabase db=helper.getReadableDatabase(); ?
? ? ? ? Cursor cursor=db.rawQuery("select count(*) from person", null); ?
? ? ? ? cursor.moveToFirst(); ?
? ? ? ? return cursor.getLong(0); ?
? ? } ?
? ? /**?
? ? ?* 分頁查詢方法 SQL語句跟MySQL的語法一樣?
? ? ?* @return?
? ? ?*/ ?
? ? public List<Person> getScrollData(int offset,int maxResult) ?
? ? { ?
? ? ? ? List<Person> persons=new ArrayList<Person>(); ?
? ? ? ? SQLiteDatabase db=helper.getReadableDatabase(); ?
? ? ? ? Cursor cursor=db.rawQuery("select * from person limit ?,?", ??
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? new String[]{String.valueOf(offset),String.valueOf(maxResult)}); ?
? ? ? ? while (cursor.moveToNext()) ?
? ? ? ? { ?
? ? ? ? ? ? int personid=cursor.getInt(cursor.getColumnIndex("personid")); ?
? ? ? ? ? ? String name=cursor.getString(cursor.getColumnIndex("name")); ?
? ? ? ? ? ? String phone=cursor.getString(cursor.getColumnIndex("phone")); ?
? ? ? ? ? ? ??
? ? ? ? ? ? persons.add(new Person(personid,name,phone)); ?
? ? ? ? } ?
? ? ? ? ??
? ? ? ? return persons; ?
? ? } ?
} ?
package com.zyq.voo; ?
public class Person ?
{ ?
? ? private Integer id; ?
? ? private String name; ?
? ? private String phone; ?
? ? ??
? ? public Person(int personid, String name, String phone) ?
? ? { ?
? ? ? ? this.id=personid; ?
? ? ? ? this.name=name; ?
? ? ? ? this.phone=phone; ?
? ? } ?
? ? ??
? ? public Person(String name, String phone) ?
? ? { ?
? ? ? ? this.name = name; ?
? ? ? ? this.phone = phone; ?
? ? } ?
? ? public String toString() ?
? ? { ?
? ? ? ? return "Person [id=" + id + ", name=" + name + ", phone=" + phone + "]"; ?
? ? } ?
? ? public Integer getId() ?
? ? { ?
? ? ? ? return id; ?
? ? } ?
? ? public void setId(Integer id) ?
? ? { ?
? ? ? ? this.id = id; ?
? ? } ?
? ? public String getName() ?
? ? { ?
? ? ? ? return name; ?
? ? } ?
? ? public void setName(String name) ?
? ? { ?
? ? ? ? this.name = name; ?
? ? } ?
? ? public String getPhone() ?
? ? { ?
? ? ? ? return phone; ?
? ? } ?
? ? public void setPhone(String phone) ?
? ? { ?
? ? ? ? this.phone = phone; ?
? ? } ?
? ? ??
? ? ??
? ? ??
} ? 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)
總結(jié)
以上是生活随笔為你收集整理的【复习】使用 SQLiteDatabase 操作 SQLite 数据库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 都在买基金定投,它的优势有哪些呢
- 下一篇: java setlocation_Jav