主java
package com.itheima.crud; import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener; import com.itheima.crud.R;
import com.itheima.crud.bean.InfoBean;
import com.itheima.crud.dao.InfoDao; public class MainActivity extends Activity implements OnClickListener { private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); mContext = this;
//创建一个帮助类对象
MySqliteOpenHelper mySqliteOpenHelper = new MySqliteOpenHelper(mContext);
//调用getReadableDatabase方法,来初始化数据库的创建
SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase(); //找到相应控件
findViewById(R.id.bt_add).setOnClickListener(this); findViewById(R.id.bt_del).setOnClickListener(this);
findViewById(R.id.bt_update).setOnClickListener(this);
findViewById(R.id.bt_query).setOnClickListener(this); }
@Override
public void onClick(View v) {
InfoDao infoDao = new InfoDao(mContext);//创建一个dao对象做增删改查 switch (v.getId()) {
case R.id.bt_add: InfoBean bean = new InfoBean();
bean.name = "张三";
bean.phone ="110";
infoDao.add(bean); InfoBean bean1 = new InfoBean();
bean1.name = "李四";
bean1.phone ="120";
infoDao.add(bean1);
break; case R.id.bt_del: infoDao.del("张三");
break; case R.id.bt_update: InfoBean bean2 = new InfoBean();
bean2.name = "张三";
bean2.phone ="119";
infoDao.update(bean2);
break; case R.id.bt_query:
infoDao.query("张三");
infoDao.query("李四");
break; default:
break;
} } }
与主java同一包下的类 继承与数据库的那个类
package com.itheima.crud; import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper; public class MySqliteOpenHelper extends SQLiteOpenHelper { public MySqliteOpenHelper(Context context) { //context :上下文 , name:数据库文件的名称 factory:用来创建cursor对象,默认为null
//version:数据库的版本号,从1开始,如果发生改变,onUpgrade方法将会调用,4.0之后只能升不能将
super(context, "info.db", null,1);
} //oncreate方法是数据库第一次创建的时候会被调用; 特别适合做表结构的初始化,需要执行sql语句;SQLiteDatabase db可以用来执行sql语句
@Override
public void onCreate(SQLiteDatabase db) {
//通过SQLiteDatabase执行一个创建表的sql语句
db.execSQL("create table info (_id integer primary key autoincrement,name varchar(20),phone varchar(11))");
} //onUpgrade数据库版本号发生改变时才会执行; 特别适合做表结构的修改
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//添加一个phone字段
// db.execSQL("alter table info add phone varchar(11)");
} }
新建包--新建类---用来增删改查
package com.itheima.crud.dao; import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import com.itheima.crud.MySqliteOpenHelper;
import com.itheima.crud.bean.InfoBean; public class InfoDao { private MySqliteOpenHelper mySqliteOpenHelper;
public InfoDao(Context context){
//创建一个帮助类对象
mySqliteOpenHelper = new MySqliteOpenHelper(context); } public void add(InfoBean bean){ //执行sql语句需要sqliteDatabase对象
//调用getReadableDatabase方法,来初始化数据库的创建
SQLiteDatabase db = mySqliteOpenHelper.getWritableDatabase();
//sql:sql语句, bindArgs:sql语句中占位符的值
db.execSQL("insert into info(name,phone) values(?,?);", new Object[]{bean.name,bean.phone});
//关闭数据库对象
db.close();
} public void del(String name){ //执行sql语句需要sqliteDatabase对象
//调用getReadableDatabase方法,来初始化数据库的创建
SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase();
//sql:sql语句, bindArgs:sql语句中占位符的值
db.execSQL("delete from info where name=?;", new Object[]{name});
//关闭数据库对象
db.close(); }
public void update(InfoBean bean){ //执行sql语句需要sqliteDatabase对象
//调用getReadableDatabase方法,来初始化数据库的创建
SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase();
//sql:sql语句, bindArgs:sql语句中占位符的值
db.execSQL("update info set phone=? where name=?;", new Object[]{bean.phone,bean.name});
//关闭数据库对象
db.close(); }
public void query(String name){ //执行sql语句需要sqliteDatabase对象
//调用getReadableDatabase方法,来初始化数据库的创建
SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase();
//sql:sql语句, selectionArgs:查询条件占位符的值,返回一个cursor对象
Cursor cursor = db.rawQuery("select _id, name,phone from info where name = ?;", new String []{name});
//解析Cursor中的数据
if(cursor != null && cursor.getCount() >0){//判断cursor中是否存在数据 //循环遍历结果集,获取每一行的内容
while(cursor.moveToNext()){//条件,游标能否定位到下一行
//获取数据
int id = cursor.getInt(0);
String name_str = cursor.getString(1);
String phone = cursor.getString(2);
System.out.println("_id:"+id+";name:"+name_str+";phone:"+phone);
}
cursor.close();//关闭结果集 }
//关闭数据库对象
db.close(); }
}
名字 电话信息包 里面就是一个类
package com.itheima.crud.dao; import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import com.itheima.crud.MySqliteOpenHelper;
import com.itheima.crud.bean.InfoBean; public class InfoDao { private MySqliteOpenHelper mySqliteOpenHelper;
public InfoDao(Context context){
//创建一个帮助类对象
mySqliteOpenHelper = new MySqliteOpenHelper(context); } public void add(InfoBean bean){ //执行sql语句需要sqliteDatabase对象
//调用getReadableDatabase方法,来初始化数据库的创建
SQLiteDatabase db = mySqliteOpenHelper.getWritableDatabase();
//sql:sql语句, bindArgs:sql语句中占位符的值
db.execSQL("insert into info(name,phone) values(?,?);", new Object[]{bean.name,bean.phone});
//关闭数据库对象
db.close();
} public void del(String name){ //执行sql语句需要sqliteDatabase对象
//调用getReadableDatabase方法,来初始化数据库的创建
SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase();
//sql:sql语句, bindArgs:sql语句中占位符的值
db.execSQL("delete from info where name=?;", new Object[]{name});
//关闭数据库对象
db.close(); }
public void update(InfoBean bean){ //执行sql语句需要sqliteDatabase对象
//调用getReadableDatabase方法,来初始化数据库的创建
SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase();
//sql:sql语句, bindArgs:sql语句中占位符的值
db.execSQL("update info set phone=? where name=?;", new Object[]{bean.phone,bean.name});
//关闭数据库对象
db.close(); }
public void query(String name){ //执行sql语句需要sqliteDatabase对象
//调用getReadableDatabase方法,来初始化数据库的创建
SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase();
//sql:sql语句, selectionArgs:查询条件占位符的值,返回一个cursor对象
Cursor cursor = db.rawQuery("select _id, name,phone from info where name = ?;", new String []{name});
//解析Cursor中的数据
if(cursor != null && cursor.getCount() >0){//判断cursor中是否存在数据 //循环遍历结果集,获取每一行的内容
while(cursor.moveToNext()){//条件,游标能否定位到下一行
//获取数据
int id = cursor.getInt(0);
String name_str = cursor.getString(1);
String phone = cursor.getString(2);
System.out.println("_id:"+id+";name:"+name_str+";phone:"+phone);
}
cursor.close();//关闭结果集 }
//关闭数据库对象
db.close(); }
}
4个按钮 xml设计
<?xml version="1.0"?> -<LinearLayout tools:context=".MainActivity" android:orientation="vertical" android:paddingTop="@dimen/activity_vertical_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android"> <Button android:id="@+id/bt_add" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/add"/> <Button android:id="@+id/bt_del" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/del"/> <Button android:id="@+id/bt_update" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/update"/> <Button android:id="@+id/bt_query" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/query"/> </LinearLayout>
老师笔记
# 2 Android下数据库第一种方式增删改查
1.创建一个帮助类的对象,调用getReadableDatabase方法,返回一个SqliteDatebase对象
2.使用SqliteDatebase对象调用execSql()做增删改,调用rawQuery方法做查询。
******特点:增删改没有返回值,不能判断sql语句是否执行成功。sql语句手动写,容易写错
private MySqliteOpenHelper mySqliteOpenHelper;
public InfoDao(Context context){
//创建一个帮助类对象
mySqliteOpenHelper = new MySqliteOpenHelper(context);
}
public void add(InfoBean bean){
//执行sql语句需要sqliteDatabase对象
//调用getReadableDatabase方法,来初始化数据库的创建
SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase();
//sql:sql语句, bindArgs:sql语句中占位符的值
db.execSQL("insert into info(name,phone) values(?,?);", new Object[]{bean.name,bean.phone});
//关闭数据库对象
db.close();
}
public void del(String name){
//执行sql语句需要sqliteDatabase对象
//调用getReadableDatabase方法,来初始化数据库的创建
SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase();
//sql:sql语句, bindArgs:sql语句中占位符的值
db.execSQL("delete from info where name=?;", new Object[]{name});
//关闭数据库对象
db.close();
}
public void update(InfoBean bean){
//执行sql语句需要sqliteDatabase对象
//调用getReadableDatabase方法,来初始化数据库的创建
SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase();
//sql:sql语句, bindArgs:sql语句中占位符的值
db.execSQL("update info set phone=? where name=?;", new Object[]{bean.phone,bean.name});
//关闭数据库对象
db.close();
}
public void query(String name){
//执行sql语句需要sqliteDatabase对象
//调用getReadableDatabase方法,来初始化数据库的创建
SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase();
//sql:sql语句, selectionArgs:查询条件占位符的值,返回一个cursor对象
Cursor cursor = db.rawQuery("select _id, name,phone from info where name = ?", new String []{name});
//解析Cursor中的数据
if(cursor != null && cursor.getCount() >0){//判断cursor中是否存在数据
//循环遍历结果集,获取每一行的内容
while(cursor.moveToNext()){//条件,游标能否定位到下一行
//获取数据
int id = cursor.getInt(0);
String name_str = cursor.getString(1);
String phone = cursor.getString(2);
System.out.println("_id:"+id+";name:"+name_str+";phone:"+phone);
}
cursor.close();//关闭结果集
}
//关闭数据库对象
db.close();
}