首先写个类继承SqliteOpenHelper
实现里面的方法
···
public class MySqlite extends SQLiteOpenHelper {
/**
-
name :数据库名称
-
factory: 游标工厂
-
version:版本号
-
作用:创建数据库
-
*/
public MySqlite(Context context){
super(context, "1711.dp", null, 3);
}
//创建表,当构造函数里面的数据库不存在的时候才会走,只会走一次
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table student(id integer primary key autoincrement,name varchar(200),age varchar(20),sex varchar(2))");}
//跟新数据库,只有版本号发生改变的时候才会走该方法,只会走一次
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.e("TAG", "oldVersion:"+oldVersion+" newVersion:"+newVersion);
db.execSQL("alter table student add pnumber int");
}
}
···
如果要给表做增删改查操作:
我们封装一个 Tools类
************************代码如下***************
···
package com.example.ytest1_ulites;
import java.util.ArrayList;
import com.example.ytest1_sqlite.MySqlite;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class Tools {
private MySqlite mySqlite;
private SQLiteDatabase wd;
//构造函数
public Tools(Context context){
//1.实例化MySqlite OpenHelper
mySqlite = new MySqlite(context);
}
// 添加数据
public Boolean add(String name,String age,String sex){
//2.调用该类的getWritableDatabase方法创建数据库
wd = mySqlite.getWritableDatabase();
//3.执行sql语句
ContentValues values = new ContentValues();
values.put("name", name);
values.put("age", age);
values.put("sex", sex);
long x = wd.insert("student", null, values);
wd.close();
mySqlite.close();
if (x>0) {
return true;
}
return false;
}
//删除数据
public Boolean delete(String name){
//2.调用该类的getWritableDatabase方法创建数据库
wd = mySqlite.getWritableDatabase();
int x = wd.delete("student", "name=?", new String[]{name});
wd.close();
mySqlite.close();
if (x>0) {
return true;
}
return false;
}
//修改数据 1.修改的值 ,2.修改的条件
public Boolean update(String age,String name){
/**
* table:表名
* values:要修改字段的值
* whereClause:要修改的条件
* whereArgs:条件的值
*/
//2.调用该类的getWritableDatabase方法创建数据库
wd = mySqlite.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("age", age);
int x = wd.update("student", values, "name=?", new String[]{name});
wd.close();
mySqlite.close();
if (x>0) {
return true;
}
return false;
}
//查看数据
public ArrayList<Student> select(){
/**
* columns:要查询的字段如果查询所有字段传null
* selection:查询的条件
* selectionArgs:查询条件的参数
* groupBy:分组,传null代表不分组
* having:分组后查询,传null代表不需要分组查询
* orderBy:排序,desc 降序 asc升序
*/
wd = mySqlite.getWritableDatabase();
ArrayList<Student> list = new ArrayList<Student>();
Cursor cursor = wd.rawQuery("select * from student", new String[]{});
cursor.moveToFirst();
cursor.moveToPrevious();
while (cursor.moveToNext()) {
int id = cursor.getInt(0);
String name = cursor.getString(1);
String age = cursor.getString(2);
String sex = cursor.getString(3);
Student student = new Student(id, name, age, sex);
list.add(student);
}
wd.close();
mySqlite.close();
return list;
}
//// 查询数据
// public ArrayList<Student> select(String name) {
// /**
// * columns:要查询的字段如果查询所有字段传null
// * selection:查询的条件
// * selectionArgs:查询条件的参数
// * groupBy:分组,传null代表不分组
// * having:分组后查询,传null代表不需要分组查询
// * orderBy:排序,desc 降序 asc升序
// */
//// 2.调用该类的getWritableDatabase方法创建数据库
// db = mySqliteOpenHelper.getWritableDatabase();
// ArrayList<Student> list = new ArrayList<Student>();
// Cursor cursor = db.query("student",null, "name=?", new String[]{name}, null, null, "id desc");
// cursor.moveToFirst();
// cursor.moveToPrevious();
// while (cursor.moveToNext()) {
// int id = cursor.getInt(0);
// String name1 = cursor.getString(1);
// int age = cursor.getInt(2);
// String sex = cursor.getString(3);
// int pnumber = cursor.getInt(4);
// Student student = new Student(id, name1, age, sex, pnumber);
// list.add(student);
//
// }
// db.close();
// mySqliteOpenHelper.close();
//
// return list;
// }
}
···
这里创建的是student表 一个表对应一个Monde类 保存数据
*****************代码如下***************
···
package com.example.ytest1_ulites;
public class Student {
private int id;
private String name;
private String age;
private String sex;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Student(int id, String name, String age, String sex) {
super();
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + ", sex="
+ sex + "]";
}
}
···
这时在MainActivity中实现创建数据库和增删改查操作
点击创建数据库按钮执行
···
findViewById(R.id.bt1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//1.实例化MySqlite
//MySqlite mySqlite = new MySqlite(MainActivity.this);
//SQLiteDatabase wd = mySqlite.getWritableDatabase();
tools= new Tools(MainActivity.this);
}
});
···
添加操作:
···
// 添加
findViewById(R.id.bt2).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
uiltes = new Uiltes(MainActivity.this);
et_1= (EditText) findViewById(R.id.et_1);
et_2= (EditText) findViewById(R.id.et_2);
et_3= (EditText) findViewById(R.id.et_3);
String name = et_1.getText().toString().trim();
String age = et_2.getText().toString().trim();
String sex = et_3.getText().toString().trim();
uiltes.add(name,age, sex);
Toast.makeText(MainActivity.this, "添加成功!", Toast.LENGTH_SHORT).show();
}
});
···
删除操作:
···
//删除数据
findViewById(R.id.bt3).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tools = new Tools(MainActivity.this);
Boolean b = tools.delete("小华");
if (b) {
Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "删除失败", Toast.LENGTH_SHORT).show();
}
}
});
···
查看操作:
···
//查看数据
findViewById(R.id.bt5).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Tools tools2 = new Tools(MainActivity.this);
ArrayList<Student> list = tools2.select();
Toast.makeText(MainActivity.this, list.toString(), Toast.LENGTH_SHORT).show();
//
// 方式二查询
// MyDbTools myDbTools = new MyDbTools(MainActivity.this);
// ArrayList<Student> list = myDbTools.select("李四");
// Toast.makeText(MainActivity.this, list.toString(), Toast.LENGTH_SHORT).show();
}
});
···
网友评论