- 论坛徽章:
- 0
|
android SQLite操作
使用Android自带的数据库SQLite,包括连接、复制、增删改查等操作。
直接看代码(直接可以运行的)
Java代码- 1.package com.named;
- 2.
- 3.import java.io.FileOutputStream;
- 4.import java.io.IOException;
- 5.import java.io.InputStream;
- 6.import java.io.OutputStream;
- 7.import java.sql.SQLException;
- 8.
- 9.import android.content.Context;
- 10.import android.database.Cursor;
- 11.import android.database.sqlite.SQLiteDatabase;
- 12.import android.database.sqlite.SQLiteException;
- 13.import android.database.sqlite.SQLiteOpenHelper;
- 14.
- 15.public class DataBaseHelper extends SQLiteOpenHelper {
- 16. private static String DB_PATH = "/data/data/com.named/databases/";
- 17. private static String DB_NAME = "mydata.db";
- 18. private static String TB_NAME = "myname";
- 19. private SQLiteDatabase myDataBase;
- 20. private final Context myContext;
- 21.
- 22. public DataBaseHelper(Context context) {
- 23. super(context, DB_NAME, null, 1);
- 24. this.myContext = context;
- 25. }
- 26.
- 27. /**
- 28. * 创建新的数据库
- 29. * */
- 30. public void createDataBase() throws IOException {
- 31. boolean dbExist = checkDataBase();
- 32. if (dbExist) {
- 33. } else {
- 34. this.getReadableDatabase();
- 35. try {
- 36. copyDataBase();
- 37. } catch (IOException e) {
- 38. throw new Error("Error copying database");
- 39. }
- 40. }
- 41. }
- 42.
- 43. /**
- 44. * 检查目录是否有数据库
- 45. *
- 46. * @return
- 47. */
- 48. private boolean checkDataBase() {
- 49. SQLiteDatabase checkDB = null;
- 50. try {
- 51. String myPath = DB_PATH + DB_NAME;
- 52. checkDB = SQLiteDatabase.openDatabase(myPath, null,
- 53. SQLiteDatabase.OPEN_READONLY);
- 54. } catch (SQLiteException e) {
- 55. // database does't exist yet.
- 56. }
- 57. if (checkDB != null) {
- 58. checkDB.close();
- 59. }
- 60. return checkDB != null ? true : false;
- 61. }
- 62.
- 63. /**
- 64. * 如果没有数据库,就把assets下面的数据库文件拷贝到sd卡data目录下
- 65. **/
- 66. private void copyDataBase() throws IOException {
- 67. InputStream myInput = myContext.getAssets().open(DB_NAME);
- 68. String outFileName = DB_PATH + DB_NAME;
- 69. OutputStream myOutput = new FileOutputStream(outFileName);
- 70. byte[] buffer = new byte[1024];
- 71. int length;
- 72. while ((length = myInput.read(buffer)) > 0) {
- 73. myOutput.write(buffer, 0, length);
- 74. }
- 75. myOutput.flush();
- 76. myOutput.close();
- 77. myInput.close();
- 78. }
- 79.
- 80. /**
- 81. * 打开数据库
- 82. *
- 83. * @throws SQLException
- 84. */
- 85. public void openDataBase() throws SQLException {
- 86. String myPath = DB_PATH + DB_NAME;
- 87. myDataBase = SQLiteDatabase.openDatabase(myPath, null,
- 88. SQLiteDatabase.OPEN_READONLY);
- 89. }
- 90.
- 91. /**
- 92. * 关闭数据库
- 93. */
- 94. @Override
- 95. public synchronized void close() {
- 96.
- 97. if (myDataBase != null)
- 98. myDataBase.close();
- 99. super.close();
- 100. }
- 101.
- 102. @Override
- 103. public void onCreate(SQLiteDatabase db) {
- 104.
- 105. }
- 106.
- 107. @Override
- 108. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- 109.
- 110. }
- 111.
- 112. /**
- 113. * 查询表数据
- 114. *
- 115. * @return
- 116. */
- 117. public String query() {
- 118. String name = "";
- 119. Cursor cursor = myDataBase.query(TB_NAME, new String[] { "name" }, "id"
- 120. + " = " + 1, null, null, null, null);
- 121. while (cursor.moveToNext()) {
- 122. name = cursor.getString(cursor.getColumnIndex("name"));
- 123. }
- 124.
- 125. return name;
- 126.
- 127. }
- 128.
- 129. /*
- 130. * 增加
- 131. */
- 132. public void save() {
- 133. myDataBase.execSQL("insert into person(name) values (?)",
- 134. new Object[] { "tom" });
- 135. }
- 136.
- 137. /*
- 138. * 更新
- 139. */
- 140. public void update() {
- 141. myDataBase.execSQL("update person set name=? where personid=?",
- 142. new Object[] { "kitty", "1" });
- 143. }
- 144.
- 145. /*
- 146. * 删除
- 147. */
- 148. public void delete(Integer id) {
- 149. myDataBase.execSQL("delete from person where personid=?",
- 150. new Object[] { id.toString() });
- 151. }
- 152.
- 153.}
复制代码 |
|