方法一:
import com.example.day014_sqliteopenhelper.db.MySqliteHelper;
import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
//将数据库的帮助类声明出来
private MySqliteHelper helper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
helper = new MySqliteHelper(MainActivity.this);
}
/**
* helper.getWritableDatabase()
*
* helper.getReadableDatabase()
*
* 磁盘没满的时候 他们两个是一样 都可以读写
* 磁盘满的时候 getWritableDatabase()打开失败
* getReadableDatabase()
* @param v
*/
public void onClick(View v){
switch (v.getId()) {
case R.id.btn_create://创建数据库
SQLiteDatabase db = helper.getWritableDatabase();
db.close();
break;
case R.id.btn_insert://创建数据库
//首先获取数据库
SQLiteDatabase db1 = helper.getWritableDatabase();
String sql_insert = "insert into person values(1,'zhangsan',23)";
String sql_insert1 = "insert into person values(2,'lisi',24)";
String sql_insert2 = "insert into person values(3,'wangwu',25)";
for(int i=4;i<20;i++){
String sql_insert3 = "insert into person values("+i+",'zhangsan"+i+"',25)";
db1.execSQL(sql_insert3);
}
//执行
db1.execSQL(sql_insert);
db1.execSQL(sql_insert1);
db1.execSQL(sql_insert2);
db1.close();
break;
case R.id.btn_query:
//首先获取数据库
SQLiteDatabase db2 = helper.getWritableDatabase();
String sql_query = "select * from person";
//指定的查询语句 cursor游标
Cursor cursor = db2.rawQuery(sql_query, null);
//判断下一条语句是否存在
while(cursor.moveToNext()){
// int id = cursor.getInt(0);
// String name = cursor.getString(1);
// int age = cursor.getInt(2);
int id = cursor.getInt(cursor.getColumnIndex("_id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
int age = cursor.getInt(cursor.getColumnIndex("age"));
System.out.println("==id==:"+id+"==name=="+name+"==age=="+age);
}
db2.close();
break;
case R.id.btn_update:
//首先获取数据库
SQLiteDatabase db3 = helper.getWritableDatabase();
String sql_update = "update person set name 'hah' where _id = 1";
db3.execSQL(sql_update);
db3.close();
break;
case R.id.btn_delete:
//首先获取数据库
SQLiteDatabase db4 = helper.getWritableDatabase();
String sql_delete = "delete from person where _id = 2";
db4.execSQL(sql_delete);
db4.close();
break;
default:
break;
}
}
}
方法二:
import com.example.day014_sqliteopenhelper.db.MySqliteHelper;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class SqliteApiActivity extends Activity{
private MySqliteHelper helper;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
helper = new MySqliteHelper(SqliteApiActivity.this);
}
public void onClick(View v){
switch (v.getId()) {
case R.id.btn_create:
//首先获取数据库
SQLiteDatabase db = helper.getWritableDatabase();
db.close();
break;
case R.id.btn_insert:
//首先获取数据库
SQLiteDatabase db1 = helper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("_id", 30);
values.put("name", "aaa");
values.put("age", 34);
db1.insert("person", null, values);
values = new ContentValues();
values.put("_id", 31);
values.put("name", "bbb");
values.put("age", 34);
/**
* 参数1:表名 参数2: 如果添加的数据是null null 参数3:添加的数据
*/
db1.insert("person", null, values);
db1.close();
break;
case R.id.btn_query:
//首先获取数据库
SQLiteDatabase db2 = helper.getWritableDatabase();
/**
* 参数1:表名
* 参数2:字段数组
* 参数3:查询的条件 _id = ?
* 参数4:查询条件的值 new String{"1"}
* 参数5:分组字段
* 参数6:在where条件后再次筛选
* 参数7:查询排序
*/
Cursor cursor = db2.query("person", null, null, null, null, null, null);
while(cursor.moveToNext()){
// int id = cursor.getInt(0);
// String name = cursor.getString(1);
// int age = cursor.getInt(2);
int id = cursor.getInt(cursor.getColumnIndex("_id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
int age = cursor.getInt(cursor.getColumnIndex("age"));
System.out.println("==id==:"+id+"==name=="+name+"==age=="+age);
}
db2.close();
break;
case R.id.btn_update:
//首先获取数据库
SQLiteDatabase db3 = helper.getWritableDatabase();
ContentValues values2 = new ContentValues();
values2.put("name", "fffff");
/**
* 参数1:表名
* 参数2:要修改的值
* 参数3:where 条件语句 _id = ?
* 参数4:条件语句的值 new String[]{"1"}
*/
int num = db3.update("person", values2, "_id = ?", new String []{"1"});
if (num>0) {
Log.i("AAAAAA","修改成功");
}else{
Log.i("AAAAAA","修改失败");
}
db3.close();
break;
case R.id.btn_delete:
//首先获取数据库
SQLiteDatabase db4 = helper.getWritableDatabase();
/**
* 参数1:数据库表名
* 参数2:where 条件语句 _id = ?
* 参数3:条件语句的值 new String[]{"1"}
*/
int num1 = db4.delete("person", "_id = ?", new String[]{1+""});
if (num1>0) {
Log.i("BBBBB","删除成功");
}else{
Log.i("BBBBB","删除失败");
}
db4.close();
break;
default:
break;
}
}
}