package com.ouling.ex_db;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class Ex_dbActivity extends Activity {
private static DB_helper mDbHelper;
private static SQLiteDatabase mdb;
private Context context;
private Button btn_newdb, btn_deldb, btn_newtable, btn_deltable, btn_add,
btn_del, btn_edit, btn_qu;
private static int i=0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = (Context) this;
mDbHelper = new DB_helper(context);
// 调用getReadableDatabase方法如果数据库不存在 则创建 如果存在则打开
mdb = mDbHelper.getReadableDatabase();
btn_newdb = (Button) findViewById(R.id.sql_newdb);
btn_deldb = (Button) findViewById(R.id.sql_deldb);
btn_newtable = (Button) findViewById(R.id.sql_newtable);
btn_deltable = (Button) findViewById(R.id.sql_deltable);
btn_add = (Button) findViewById(R.id.sql_add);
btn_del = (Button) findViewById(R.id.sql_delete);
btn_edit = (Button) findViewById(R.id.sql_edit);
btn_qu = (Button) findViewById(R.id.sql_qu);
// 数据库
btn_newdb.setOnClickListener(listener);
btn_deldb.setOnClickListener(listener);
// 表
btn_newtable.setOnClickListener(listener);
btn_deltable.setOnClickListener(listener);
// 数据
btn_add.setOnClickListener(listener);
btn_del.setOnClickListener(listener);
btn_edit.setOnClickListener(listener);
btn_qu.setOnClickListener(listener);
}
final OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
switch (v.getId()) {
// 新建数据库
case R.id.sql_newdb:
mDbHelper = new DB_helper(context);
// 调用getReadableDatabase方法如果数据库不存在 则创建 如果存在则打开
mdb = mDbHelper.getReadableDatabase();
// // ---------------以下两个成员变量是针对在SD卡中存储数据库文件使用
// File path = new File("/sdcard/ouling");// 创建目录
// File f = new File("/sdcard/ouling/OuLing.db");// 创建文件
// // 如果你使用的是将数据库的文件创建在SD卡中,那么创建数据库mysql如下操作:
// if (!path.exists()) {// 目录存在返回false
// path.mkdirs();// 创建一个目录
// }
// if (!f.exists()) {// 文件存在返回false
// try {
// f.createNewFile();// 创建文件
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
// 如果你使用的是将数据库的文件创建在SD卡中,那么创建数据库mysql如下操作:
// mdb = SQLiteDatabase.openOrCreateDatabase(f, null);
Toast.makeText(context, "成功创建数据库", 1000).show();
break;
// 删除数据库
case R.id.sql_deldb:
mDbHelper = new DB_helper(context);
mdb = mDbHelper.getReadableDatabase();
// 关闭数据库
mdb.close();
// 删除数据库
if (context.deleteDatabase(DB_helper.DATABASE_NAME)) {
Toast.makeText(context, "成功删除数据库", 1000).show();
}
// // ---------------以下是删除SD卡中数据库
// File del_f = new File("/sdcard/ouling/OuLing.db");// 创建文件
// if (del_f.exists()) {
// del_f.delete();
// }
// Toast.makeText(context, "成功删除数据库", 1000).show();
break;
// 新建表
case R.id.sql_newtable:
try {
mdb = mDbHelper.getWritableDatabase();
String str_sql = "CREATE TABLE New_table(id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR);";
mdb.execSQL(str_sql);
Toast.makeText(context, "成功创建一张新表", 1000).show();
} catch (Exception e) {
Toast.makeText(context, "该表已经存在", 1000).show();
}
break;
// 删除表
case R.id.sql_deltable:
try {
mdb = mDbHelper.getWritableDatabase();
String str_sql = "DROP TABLE New_table";
mdb.execSQL(str_sql);
Toast.makeText(context, "成功删除表", 1000).show();
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(context, "要求删除的表不存在", 1000).show();
}
break;
// 添加数据
case R.id.sql_add:
mdb = mDbHelper.getWritableDatabase();
// ---------------------- 读写句柄来插入---------
// ContentValues 其实就是一个哈希表HashMap, key值是字段名称,
// Value值是字段的值。然后 通过 ContentValues 的 put 方法就可以
// 把数据放到ContentValues中,然后插入到表中去!
ContentValues add_cv = new ContentValues();
add_cv.put("name", "欧零"+i+"");
add_cv.put("number", i+"");
mdb.insert("student_info", null, add_cv);
// inser() 第一个参数 标识需要插入操作的表名
// 第二个参数 :默认传null即可
// 第三个是插入的数据
// ---------------------- SQL语句插入--------------
// String INSERT_DATA =
// "INSERT INTO student_info(id,text) values (1, '通过SQL语句插入')";
// mdb.execSQL(INSERT_DATA);
Toast.makeText(context, "成功添加数据", 1000).show();
break;
// 删除数据
case R.id.sql_delete:
mdb = mDbHelper.getWritableDatabase();
// ---------------------- 读写句柄来删除
// mdb.delete("student_info", "number =1", null);
// 第一个参数 需要操作的表名
// 第二个参数为 id+操作的下标 如果这里我们传入null,表示全部删除
// 第三个参数默认传null即可
// ----------------------- SQL语句来删除
String DELETE_DATA = "DELETE FROM student_info WHERE number=1";
mdb.execSQL(DELETE_DATA);
Toast.makeText(context, "成功删除数据", 1000).show();
break;
// 修改数据
case R.id.sql_edit:
mdb = mDbHelper.getWritableDatabase();
// ------------------------句柄方式来修改 -------------
ContentValues edit_cv = new ContentValues();
edit_cv.put("name", "示例大全");
mdb.update("student_info", edit_cv, "id = 2", null);
// ------------------------SQL语句来修改 -------------
// String UPDATA_DATA =
// "UPDATE student_info SET text='通过SQL语句的示例大全' WHERE id=2";
// db.execSQL(UPDATA_DATA);
Toast.makeText(context, "成功修改数据", 1000).show();
break;
// 遍历数据
case R.id.sql_qu:
mdb = mDbHelper.getReadableDatabase();
// ------------------------语句查询
// Cursor cur = mdb.rawQuery("SELECT * FROM "
// + DB_helper.TABLE_NAME, null);
// ------------------------句柄查询
String[] projections = new String[] { "id", "name","number" };
Cursor cur = mdb.query("student_info", projections, null, null,
null, null, "id desc ");
String temp = "";
if (cur != null) {
if (cur.getCount() == 0) {
temp = "无数据";
}
while (cur.moveToNext()) {// 直到返回false说明表中到了数据末尾
temp += cur.getString(0) + " ";
// 参数0 指的是projections数组列的下标,这里的0指的是id列
temp += cur.getString(1);
// 这里的1应该是咱们的name列
temp += cur.getString(2);
// 这里的2对应的是number列
temp += "\n";
}
}
Toast.makeText(context, temp, 1000).show();
break;
default:
break;
}
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.toString());
} finally {
// 如果发生异常,同样需要对数据库进行关闭
mdb.close();
}
}
};
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="数据库操作:" />
<Button android:id="@+id/sql_newdb" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="新建数据库"></Button>
<Button android:id="@+id/sql_deldb" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="删除数据库"></Button>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="表操作:" />
<Button android:id="@+id/sql_newtable" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="新建一张数据表"></Button>
<Button android:id="@+id/sql_deltable" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="删除一张数据表"></Button>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="数据操作:" />
<Button android:id="@+id/sql_add" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="表中添加一条记录"></Button>
<Button android:id="@+id/sql_delete" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="表中删除一条记录"></Button>
<Button android:id="@+id/sql_edit" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="表中修改一条记录"></Button>
<Button android:id="@+id/sql_qu" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="查询表中所有记录"></Button>
</LinearLayout>