首先Database 数据库创建
1.创建一个Database包,包内创建一个MyDatabaseHelper继承DatabaseHelper类,并实现里面的方法!
3 import android.content.Context; 4 import android.database.sqlite.SQLiteDatabase; 5 import android.database.sqlite.SQLiteOpenHelper; 6 import android.widget.Toast; 7 8 /** 9 * 数据库帮助类 10 * Created by Administrator on 2017/12/6. 11 */ 12 13 public class MyDatabaseHelper extends SQLiteOpenHelper{ 14 15 public static final String CREATE_USERINFO="CREATE TABLE USERINFO(" //创建表的SQL语句 16 + "id integer primary key autoincrement, " //id 设置主键 17 + "username text, " //用户名 18 + "age integer, " //年龄 19 + "sex text, " //性别 20 + "mAutograph text)"; //签名 21 private Context mContext; 22 23 24 /** 25 * 生成的构造方法 26 * @param context 获得上下文 27 * @param name 28 * @param factory 29 * @param version 30 */ 31 public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { 32 super(context, name, factory, version); 33 mContext=context; //获取上下文 34 } 35 36 37 /** 38 * 实现方法 39 * 创建数据库 40 * @param db 41 */ 42 @Override 43 public void onCreate(SQLiteDatabase db) { 44 db.execSQL(CREATE_USERINFO); //创建数据库,参数为创表语句 45 Toast.makeText(mContext,"创建表成功",Toast.LENGTH_LONG).show(); 46 } 47 48 /** 49 * 实现方法 50 * 升级数据库 51 * @param sqLiteDatabase 52 * @param i 53 * @param i1 54 */ 55 @Override 56 public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { 57 58 } 59 }
2.升级数据库,
1 /** 2 * 实现方法 3 * 升级数据库 4 * @param sqLiteDatabase 5 * @param i 6 * @param i1 7 */ 8 @Override 9 public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { 10 sqLiteDatabase.execSQL("drop table if exists CREATE_USERINFO"); //删除数据库 11 sqLiteDatabase.execSQL("drop table if exists CREATE_CONTACT"); //删除数据库 12 } 13 14 private MyDatabaseHelper mDBhelper; //声明数据库帮助类变量 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.activity_main); 19 mDBhelper=new MyDatabaseHelper(this,"this,UserInfo",null,3); //3代表升级 20 } 25 /** 26 * 启动时创建数据库 27 */ 28 @Override 29 protected void onStart() { 30 super.onStart(); 31 mDBhelper.getWritableDatabase(); //获得可写的数据库,会创建helper数据库 32 }
3.查询数据库
1 @Override 2 public List<UserInfo> getUserInfo(MyDatabaseHelper db) { 3 SQLiteDatabase sqlhelper=db.getWritableDatabase(); 4 Cursor cursor=sqlhelper.query("USERINFO",null,null,null,null,null,null); 5 UserInfo user; 6 List<UserInfo> list=new ArrayList<>(); 7 if(cursor.moveToFirst()){ 8 do{ 9 String name=cursor.getString(cursor.getColumnIndex("username")); 10 String age=cursor.getString(cursor.getColumnIndex("age")); 11 String sex=cursor.getString(cursor.getColumnIndex("sex")); 12 String phone=cursor.getString(cursor.getColumnIndex("Phone")); 13 user=new UserInfo(name,age,sex,phone); 14 list.add(user); 15 }while (cursor.moveToNext()); 16 } 17 cursor.close(); 18 return list; 19 }
4.插入数据库 MyDatabaseHelper database继承至DatabaseHelper
1 @Override 2 public void setUserInfo(MyDatabaseHelper database,UserInfo user) { 3 SQLiteDatabase db=database.getWritableDatabase(); 4 ContentValues values=new ContentValues(); 5 values.put("username",user.getName()); 6 values.put("age",user.getAge()); 7 values.put("sex",user.getSex()); 8 values.put("Phone",user.getPhone()); 9 db.insert("USERINFO",null,values); 10 }
5.修改数据库
1 @Override 2 public void DelectUserInfo(MyDatabaseHelper database) { 3 SQLiteDatabase sqLiteDatabase=database.getWritableDatabase(); 4 ContentValues values=new ContentValues(); 5 values.put("price",10.99); //所需要修改的字段 price 字段 10.99值 6 7 //修改数据 ?站位符 new String[]{修改?数据} 8 sqLiteDatabase.update("USERINFO",values,"name=?",new String[]{"TheDaviciCode"}); 9 }
6.删除数据
1 @Override 2 public void DelectUserInfo(MyDatabaseHelper database) { 3 //删除ID小于5的数据 4 SQLiteDatabase db=database.getWritableDatabase(); 5 db.delete("USERINFO","id > 5",new String[]{"5"}); 6 }