[关闭]
@Yano 2016-03-21T09:22:02.000000Z 字数 2331 阅读 2049

Android 数据库

Android


功能

实现数据库基本的增删改查

代码

MainActivity:

  1. public class MainActivity extends Activity {
  2. private Button button1, button2, button3, button4;
  3. private static MySqlHelper mySqlHelper;
  4. private static SQLiteDatabase db;
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9. mySqlHelper = new MySqlHelper(MainActivity.this, "student_inf.db",
  10. null, 1);
  11. db = mySqlHelper.getWritableDatabase();
  12. button1 = (Button) findViewById(R.id.button1);
  13. button2 = (Button) findViewById(R.id.button2);
  14. button3 = (Button) findViewById(R.id.button3);
  15. button4 = (Button) findViewById(R.id.button4);
  16. // 添加数据
  17. button1.setOnClickListener(new View.OnClickListener() {
  18. @Override
  19. public void onClick(View v) {
  20. // 加入2个数据
  21. ContentValues values = new ContentValues();
  22. values.put("name", "stu01");
  23. values.put("password", "abc");
  24. db.insert("student", null, values);
  25. ContentValues values2 = new ContentValues();
  26. values2.put("name", "stu02");
  27. values2.put("password", "def");
  28. db.insert("student", null, values2);
  29. query();
  30. }
  31. });
  32. // 删除数据
  33. button2.setOnClickListener(new View.OnClickListener() {
  34. @Override
  35. public void onClick(View v) {
  36. Cursor cursor = db.rawQuery(
  37. "delete from student where name = '" + "stu01" + "'",
  38. null);
  39. cursor.moveToNext();
  40. query();
  41. }
  42. });
  43. // 修改数据
  44. button3.setOnClickListener(new View.OnClickListener() {
  45. @Override
  46. public void onClick(View v) {
  47. ContentValues values = new ContentValues();
  48. values.put("name", "stu03");
  49. values.put("password", "hig");
  50. db.update("student", values, "name=?", new String[] { "stu01" });
  51. query();
  52. }
  53. });
  54. // 查询数据
  55. button4.setOnClickListener(new View.OnClickListener() {
  56. @Override
  57. public void onClick(View v) {
  58. query();
  59. }
  60. });
  61. }
  62. // 打印数据库中所有的name
  63. private static void query() {
  64. Cursor cursor = db.rawQuery("select * from student", null);
  65. System.out.println("打印数据库中所有的name");
  66. while (cursor.moveToNext()) {
  67. System.out.println(cursor.getString(1));
  68. }
  69. cursor.close();
  70. }
  71. }

MySqlHelper:创建数据库表 student,其中包括 namepassword两列。注意 execSQL 语句的格式,结尾是有个括号的!

  1. public class MySqlHelper extends SQLiteOpenHelper {
  2. public MySqlHelper(Context context, String name, CursorFactory factory,
  3. int version) {
  4. super(context, name, factory, version);
  5. }
  6. @Override
  7. public void onCreate(SQLiteDatabase db) {
  8. // 创建数据库表 student
  9. db.execSQL("create table student(_id integer primary key autoincrement,"
  10. + "name varchar(20)," + "password varchar(20))");
  11. System.out.println("onCreate 被调用");
  12. }
  13. @Override
  14. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  15. System.out.println("onUpgrade 被调用" + oldVersion + "--" + newVersion);
  16. }
  17. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注