admin 管理员组

文章数量: 887060


2024年1月19日发(作者:phpimplode函数)

android中SQLite的使用总结,用excSQL和rawQuery方法实现一般得增删改查

1:的内容

[html] view plaincopy

1.

2.

3. package=""

4. android:versionCode="1"

5. android:versionName="1.0">

6.

7.

8.

9. android:label="@string/app_name">

10.

11.

12.

13.

14.

15.

16.

17.

18.

19.

20.

21. android:targetPackage=""

22. android:label="Test for My App"/>

23.

24.

2:Person类

[java] view plaincopy

1. package ;

2.

3. public class Person {

4.

5. private Integer id;

6. private String name;

7.

8. public Person() {

9.

10. }

11.

12. public Integer getId() {

13. return id;

14. }

15.

16. public void setId(Integer id) {

17. = id;

18. }

19.

20. public String getName() {

21. return name;

22. }

23.

24. public void setName(String name) {

25. = name;

26. }

27.

28. @Override

29. public String toString() {

30. return "Person [id=" + id + ", name=" + name + "]";

31. }

32.

33. }

3:DBOpenHelper类,该类继承了SQLiteOpenHelper类

[java] view plaincopy

1. package e;

2.

3. import t;

4. import Database;

5. import OpenHelper;

6. import Factory;

7.

8. public class DBOpenHelper extends SQLiteOpenHelper {

9.

10. private static final String DATABASENAME = "";

11. private static final int DATABASEVERSION = 1;

12.

13. /*

14. * 构造函数

15. */

16. public DBOpenHelper(Context context) {

17. super(context, DATABASENAME, null, DATABASEVERSI

ON);

18. }

19.

20. /*

21. * 数据库第一次生成时调用该方法,创建一些表或者初始化一些数据

22. * @see OpenHelper#onCreate(Database)

23. */

24. @Override

25. public void onCreate(SQLiteDatabase db) {

26. L("create table person(personid integer primary key autoincrement, name varchar(20))");

27. }

28.

29. @Override

30. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

31.

32. }

33.

34. }

4:PersonService类

[java] view plaincopy

1. package e;

2.

3. import ist;

4. import ;

5.

6. import t;

7. import ;

8. import Database;

9. import ;

10.

11. public class PersonService {

12.

13. private DBOpenHelper dbOpenHelper;

14.

15. public PersonService(Context context) {

16. Helper = new DBOpenHelper(context);

17. }

18.

19. /*

20. * save a person to the database

21. */

22. public void save(Person person) {

23. SQLiteDatabase database = tableDatabase();

24. L("insert into person(name) values (?)", new Object[]{e()});

25. }

26.

27. /*

28. * updata a person to the database

29. */

30. public void update(Person person) {

31. SQLiteDatabase database = tableDatabase();

32. L("update person set name=? where

personid=?", new Object[]{e(), ()});

33. }

34.

35. /*

36. * delete a person from the database according to the id

37. */

38. public void delete(Integer id) {

39. SQLiteDatabase database = tableDatabase();

40. L("delete from person where personid=?", new Object[]{ng()});

41. }

42.

43. /*

44. * find a person from the database according to the id

45. */

46. public Person find(Integer id) {

47. SQLiteDatabase database = dableDatabase();

48. Cursor cursor = ry("select * from person where personid=?", new String[]{ng()});

49. Person person = null;

50. if(First()) {

51. Integer personid = (umnIndex("personid"));

52. String name = ing(umnIndex("name"));

53. person = new Person();

54. (personid);

55. e(name);

56. }

57. return person;

58. }

59.

60. /*

61. * get the data of person accroding to the offset and maxResult

62. */

63. public List getScrollData(Integer offset, Integer maxResult) {

64. SQLiteDatabase database = dableDatabase();

65. Cursor cursor = ry("select * from person limit ?,?", new String[] {ng(), ng()});

66. int idIndex = 0;

67. int nameIndex = 0;

68. List personList = null;

69.

70. if(nt() >= 0) {

71. idIndex = umnIndex("personid");

72. nameIndex = umnIndex("name");

73. personList = new ArrayList();

74. }

75.

76. while(Next()) {

77. Integer personid = (idIndex);

78. String name = ing(nameIndex);

79. Person person = new Person();

80. (personid);

81. e(name);

82. (person);

83. }

84. return personList;

85. }

86.

87. /*

88. * get the count of the database

89. */

90. public long getCount(){

91. SQLiteDatabase database = dableDatabase();

92. Cursor cursor = ry("select count(*) from person", null);

93. First();

94. return g(0);

95. }

96.

97. }

5:PersonServiceTest类

[java] view plaincopy

1. package ;

2.

3. import ;

4.

5. import dTestCase;

6. import ;

7. import ;

8. import Helper;

9. import Service;

10.

11. public class PersonServiceTest extends AndroidTestCase {

12. private static final String TAG = "PersonServiceTest";

13.

14. /*

15. * 测试生成数据库的方法

16. */

17. public void testCreateDB() throws Throwable {

18. DBOpenHelper dbOpenHelper = new DBOpenHelper(text());

19. tableDatabase(); //第一次调用该方法会生成数据库

20.

21. }

22.

23. /*

24. * 测试保存方法

25. */

26. public void testSave() throws Throwable{

27. PersonService personService = new PersonService(text());

28.

29. Person person1 = new Person();

30. e("zhangsan");

31. (person1);

32.

33. Person person2 = new Person();

34. e("lisi");

35. (person2);

36.

37. Person person3 = new Person();

38. e("wangwu");

39. (person3);

40.

41. }

42.

43. public void testDelete() {

44. PersonService personService = new PersonService(text());

45. (1);

46.

47. }

48.

49. /*

50. * 测试更新方法

51. */

52. public void testUpdate() {

53. PersonService personService = new PersonService(text());

54. Person person = (1);

55. e("zhaoliu");

56. (person);

57.

58. }

59.

60. /*

61. * 测试获得数据方法

62. */

63. public void testGetScrollData() throws Throwable{

64. PersonService personService = new PersonService(text());

65. List persons = ollData(0, 3);

66. for(Person person : persons) {

67. Log.i(TAG, ng());

68. }

69. }

70.

71. /*

72. * 测试根据id查找的方法

73. */

74. public void testFind() throws Throwable{

75. PersonService personService = new PersonService(text());

76. Person person = (1);

77. Log.i(TAG, ng());

78.

79. }

80.

81. /*

82. * 测试获得数量的方法

83. */

84. public void testGetCount() {

85. PersonService personService = new PersonService(text());

86. long count = nt();

87. Log.i(TAG, count + "");

88. }

89. }


本文标签: 方法 测试 生成 数据库