- 创建数据库 建表 查询语句
public class DbSqliteOpenHelper extends SQLiteOpenHelper {
private static final String TAG = "DbSqliteOpenHelper";
//更改构造方法
public DbSqliteOpenHelper(Context context) {
super(context, "student.db", null, 1);
// TODO Auto-generated constructor stub
}
//创建数据库
/**
* students 表名
* _id 标识
* name 姓名
* sex 年龄
* love 爱好
*/
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table students(_id integer primary key, name varchar(30),sex integer,love varchar(100))");
Log.i(TAG, "onCreate 方法执行了");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
- 增 删 改 查的方法
public class StudentDao {
private static final String TAG = "StudentDao";
private DbSqliteOpenHelper helper;
//构造方法
public StudentDao(Context context){
helper=new DbSqliteOpenHelper(context);
}
/**
* 插入方法
* @param name 姓名
* @param sex 性别
* @param love 爱好
*/
public long insert(String name, int sex, String love) {
// 获取数据
SQLiteDatabase db = helper.getWritableDatabase();
// 填充占位符
ContentValues values = new ContentValues();
values.put("name", name);
values.put("sex", sex);
values.put("love", love);
String nullColumnHack = "values(null,?,?,?)";
// 执行SQL
long insert = db.insert("students", nullColumnHack, values);
// 关闭数据库连接
db.close();
return insert;
}
/**
* 修改更改 update
* @param name 姓名
* @param sex 性别
* @param love 爱好
*/
public int update(String name, int sex, String love) {
// 获取数据
SQLiteDatabase db = helper.getWritableDatabase();
String sql = "update students set sex=?,love=? where name=?";
// 填充占位符
ContentValues values = new ContentValues();
values.put("sex", sex);
values.put("love", love);
// 执行SQL
int update = db.update("students", values, " name=?", new String[]{name});
// 关闭数据库连接
db.close();
return update;
}
/**
* 删除
* @param name 姓名
*/
public int delete(String name) {
// 获取数据
SQLiteDatabase db = helper.getWritableDatabase();
// 执行SQL
int delete = db.delete("students", " name=?", new String[]{name});
// 关闭数据库连接
db.close();
return delete;
}
/**
* 查询全部
*
*/
public void getAll(){
//获取数据
SQLiteDatabase db = helper.getReadableDatabase();
String sql="select _id,name,sex,like from students";
//执行SQL
Cursor cursor = db.rawQuery(sql, null);
while(cursor.moveToNext()){
int _id = cursor.getInt(cursor.getColumnIndex("_id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
int sex = cursor.getInt(cursor.getColumnIndex("sex"));
String like = cursor.getString(cursor.getColumnIndex("like"));
Log.i(TAG+"======", "_id:"+_id+"name:"+name+",sex:"+sex+",like:"+like);
}
cursor.close();
//关闭数据库连接
db.close();
}
/**
* 查询全部
* stuname 查询名字相同的所有数据
*/
public void getOne(String stuname){
//获取数据
SQLiteDatabase db = helper.getReadableDatabase();
String sql="select _id,name,sex,like from students where name=?";
String[] selectionArgs = {stuname};
//执行SQL
Cursor cursor = db.rawQuery(sql, selectionArgs);
while(cursor.moveToNext()){
int _id = cursor.getInt(cursor.getColumnIndex("_id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String sex = cursor.getString(cursor.getColumnIndex("sex"));
String like = cursor.getString(cursor.getColumnIndex("like"));
Log.i(TAG, "_id:"+_id+"name:"+name+",sex:"+sex+",like:"+like);
}
cursor.close();
//关闭数据库连接
db.close();
}
}
3.调用
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DbSqliteOpenHelper mHelper=new DbSqliteOpenHelper(this);
//一定要调用getWritableDatabase(),
**//才是真正的创建了数据库 又是坑啊**
mHelper.getWritableDatabase();
StudentDao dao=new StudentDao(this);
// dao.insert("易皇星", 24, "喜欢运动");
// dao.getOne("易皇星");
dao.update("易黄星",20 , "听歌");
// dao.delete("易皇星");
// dao.getAll();
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/12883.html