首页 > 代码库 > 练习使用SQLite实现数据存储
练习使用SQLite实现数据存储
第一个Demo:
1 package com.example.sqlitedemo; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.database.Cursor; 6 import android.database.sqlite.SQLiteDatabase; 7 import android.util.Log; 8 import android.view.Menu; 9 10 public class MainActivity extends Activity {11 12 @Override13 protected void onCreate(Bundle savedInstanceState) {14 super.onCreate(savedInstanceState);15 setContentView(R.layout.activity_main);16 SQLiteDatabase db = openOrCreateDatabase("user.db", MODE_PRIVATE, null);17 db.execSQL("create table if not exists usertb(_id integer primary key autoincrement,name text not null,age integer not null,sex text not null)");18 db.execSQL("insert into usertb(name,sex,age) values (‘阿狸‘,‘女‘,18)");19 db.execSQL("insert into usertb(name,sex,age) values (‘阿Q‘,‘男‘,32)");20 db.execSQL("insert into usertb(name,sex,age) values (‘小学生‘,‘男‘,9)");21 db.execSQL("insert into usertb(name,sex,age) values (‘橙哥‘,‘男‘,16)");22 Cursor c = db.rawQuery("select * from usertb", null);23 if(c != null) {24 while(c.moveToNext()) {25 Log.i("info", ""+c.getInt(c.getColumnIndex("_id")));26 Log.i("info", ""+c.getString(c.getColumnIndex("name")));27 Log.i("info", ""+c.getString(c.getColumnIndex("age")));28 Log.i("info", ""+c.getInt(c.getColumnIndex("age")));29 Log.i("info","!!!!!!");30 }31 c.close();32 }33 db.close();34 }35 36 }
使用原生SQL语句实现数据插入,并在Log中查询数据。
Demo2:
1 package com.example.sqlitedemo2; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.content.ContentValues; 6 import android.database.Cursor; 7 import android.database.sqlite.SQLiteDatabase; 8 import android.util.Log; 9 import android.view.Menu;10 11 public class MainActivity extends Activity {12 13 @Override14 protected void onCreate(Bundle savedInstanceState) {15 super.onCreate(savedInstanceState);16 setContentView(R.layout.activity_main);17 SQLiteDatabase db = openOrCreateDatabase("stu.db", MODE_PRIVATE, null);18 db.execSQL("create table if not exists stutb(_id integer primary key autoincrement,name text not null,sex text not null,age integer not null)");19 ContentValues values = new ContentValues();20 values.put("name", "阿狸");21 values.put("sex", "男");22 values.put("age", 18);23 db.insert("stutb", null, values);24 values.clear();25 values.put("name", "Asuna");26 values.put("sex", "男");27 values.put("age", 16);28 db.insert("stutb", null, values);29 values.clear();30 values.put("name", "Siber");31 values.put("sex", "男");32 values.put("age", 17);33 db.insert("stutb", null, values);34 values.clear();35 values.put("name", "阿Q");36 values.put("sex", "男");37 values.put("age", 32);38 db.insert("stutb", null, values);39 values.clear();40 values.put("name", "Lucifer");41 values.put("sex", "男");42 values.put("age", 99999);43 db.insert("stutb", null, values);44 values.clear();45 values.put("sex", "女");46 db.update("stutb", values, "_id<=?", new String[]{"3"});47 db.delete("stutb", "name like ?", new String[]{"%Lu%"});48 Cursor c = db.query("stutb", null, "_id>?", new String[]{"0"}, null, null, "age");49 if(c != null) {50 String[] columns = c.getColumnNames();51 while(c.moveToNext()) {52 for(String columnName : columns) {53 Log.i("info", c.getString(c.getColumnIndex(columnName)));54 }55 }56 c.close();57 }58 db.close();59 }60 61 }
使用ContentValues载入键值对进行数据操作。
Demo3:
MainActivity:
1 package com.example.sqlitedemo3; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.database.Cursor; 6 import android.database.sqlite.SQLiteDatabase; 7 import android.util.Log; 8 import android.view.Menu; 9 10 public class MainActivity extends Activity {11 12 @Override13 protected void onCreate(Bundle savedInstanceState) {14 super.onCreate(savedInstanceState);15 setContentView(R.layout.activity_main);16 DBOpenHelper helper = new DBOpenHelper(MainActivity.this, "stu.db");17 // helper.getReadableDatabase();18 SQLiteDatabase db = helper.getWritableDatabase();19 Cursor c = db.rawQuery("select * from stutb", null);20 if(c != null) {21 String[] columnName = c.getColumnNames();22 while(c.moveToNext()) {23 for(String columns:columnName) {24 Log.i("info", c.getString(c.getColumnIndex(columns)));25 }26 }27 c.close();28 }29 db.close();30 }31 }
DBOpenHelper:
1 package com.example.sqlitedemo3; 2 3 import android.content.ContentValues; 4 import android.content.Context; 5 import android.database.sqlite.SQLiteDatabase; 6 import android.database.sqlite.SQLiteDatabase.CursorFactory; 7 import android.database.sqlite.SQLiteOpenHelper; 8 9 public class DBOpenHelper extends SQLiteOpenHelper {10 11 public DBOpenHelper(Context context, String name) {12 super(context, name, null, 1);13 // TODO Auto-generated constructor stub14 }15 16 public DBOpenHelper(Context context, String name, CursorFactory factory,17 int version) {18 super(context, name, factory, version);19 // TODO Auto-generated constructor stub20 }21 22 @Override23 public void onCreate(SQLiteDatabase db) {24 // TODO Auto-generated method stub25 db.execSQL("create table if not exists stutb(_id integer primary key,name text not null,sex text not null,age integer not null)");26 db.execSQL("insert into stutb(name,sex,age) values(‘Nico‘,‘female‘,16)");27 }28 29 @Override30 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {31 // TODO Auto-generated method stub32 33 }34 35 }
使用SQLiteOpenHelper类实现数据库的创建、更新等操作。
生成的数据库导出后可用NaviCat软件查看信息。
练习使用SQLite实现数据存储
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。