数据库的基本操作
1.这是数据库操作的一个帮助类,用来创建数据库
package com.kotei.operation;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DataBaseOpenHelp extends SQLiteOpenHelper {
private static final String name = "guotu";
private static final int version = 1;
public DataBaseOpenHelp(Context context) {
super(context, name, null, version);
}
/**
* taskSchedule:任务列表
* 1.task_id:任务ID
* 2.task_name:任务的名字
* 3.task_type:任务的类型
* 4.sendman_name:发送人的姓名
* 5.task_get_time:任务获取时间
* 6.task_state:任务的状态
* 7.task_content:任务的内容
* 8.spareattribute1:备用属性
* 9.spareattribute2:备用属性
*
*/
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table taskSchedule (task_id integer primary key autoincrement,task_name varchar(20),task_type varchar(20),sendman_name varchar(20),task_get_time varchar(30),task_state varchar(10),task_content varchar(500),spareattribute1 varchar(20),spareattribute2 varchar(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists taskSchedule");
onCreate(db);
}
}
2.数据库增删查改的操作
package com.kotei.show;
import com.kotei.operation.DataBaseOpenHelp;
import android.app.Activity;
import android.content.ContentValues;
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.EditText;
import android.widget.TextView;
public class SQLitTestActivity extends Activity {
private DataBaseOpenHelp dboh;
private SQLiteDatabase sd;
private TextView dbinfo;
private Button bt_check;
private Button bt_insert;
private Button bt_update;
private Button bt_delet;
private Button bt_conditionscheck;
private EditText et_check;
private EditText et_insert;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bt_insert = (Button) findViewById(R.id.bt_insert);
bt_check = (Button) findViewById(R.id.bt_check);
bt_update = (Button) findViewById(R.id.bt_update);
bt_delet = (Button) findViewById(R.id.bt_delet);
bt_conditionscheck = (Button) findViewById(R.id.bt_conditionscheck);
dbinfo = (TextView) findViewById(R.id.dbinfo);
bt_insert.setOnClickListener(listener);
bt_check.setOnClickListener(listener);
bt_update.setOnClickListener(listener);
bt_delet.setOnClickListener(listener);
bt_conditionscheck.setOnClickListener(listener);
dboh = new DataBaseOpenHelp(SQLitTestActivity.this);
}
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_check:
String text = check(2);
dbinfo.setText(text);
break;
case R.id.bt_insert:
insert();
break;
case R.id.bt_update:
update();
break;
case R.id.bt_delet:
delet();
break;
case R.id.bt_conditionscheck:
conditionsCheck();
break;
default:
break;
}
}
};
/**
* 保存数据到数据表
*
* @creattime 2012-3-13
*
*/
private void insert() {
sd = dboh.getWritableDatabase();
sd.execSQL(
"insert into taskSchedule(task_name,task_type,sendman_name,task_get_time,task_state,task_content,spareattribute1,spareattribute2) values(?,?,?,?,?,?,?,?)",
new Object[] { "戴国伟的短信调度", "短信调度", "小猪", "2012-3-13 14:30:29",
"新下达", "去派出所收保护费", null, null });
sd.close();
}
/**
* 查询数据
*
* @creattime 2012-3-13
*
* @param 要查询的数据的id
* @return
*/
public String check(int id) {
sd = dboh.getReadableDatabase();
// 以task_id查询所有数据
Cursor cursor = sd.rawQuery("select * from taskSchedule where task_id = ?", new String[] { String.valueOf(id) });
while (cursor.moveToNext()) {
// 第一种方法查询所有数据
String s = cursor.getInt(0) + "," + cursor.getString(1) + ","
+ cursor.getString(2) + "," + cursor.getString(3) + ","
+ cursor.getString(4) + "," + cursor.getString(5) + ","
+ cursor.getString(6) + "," + cursor.getString(7) + ","
+ cursor.getString(8);
// 第二种方法查询所有数据
String s2 = cursor.getString(cursor.getColumnIndex("task_id"))
+ cursor.getString(cursor.getColumnIndex("task_name"))
+ cursor.getString(cursor.getColumnIndex("task_type"))
+ cursor.getString(cursor.getColumnIndex("sendman_name"))
+ cursor.getString(cursor.getColumnIndex("task_get_time"))
+ cursor.getString(cursor.getColumnIndex("task_state"))
+ cursor.getString(cursor.getColumnIndex("task_content"))
+ cursor.getString(cursor.getColumnIndex("spareattribute1"))
+ cursor.getString(cursor.getColumnIndex("spareattribute2"));
return s;
}
cursor.close();
sd.close();
return null;
}
/**
* 更新数据
*
* @creattime 2012-3-13
*
*/
public void update() {
sd = dboh.getWritableDatabase();
ContentValues cv = new ContentValues();
// 将"task_name"字段的值修改成为"奥巴马的任务"
cv.put("task_name", "奥巴马的任务");
sd.update("taskSchedule", cv, "task_id=?", new String[] { String.valueOf(2)});
cv.clear();
sd.close();
}
/**
* 删除数据
*
* @creattime 2012-3-13
*
*/
public void delet(){
sd = dboh.getWritableDatabase();
// 删除task_id=1的记录
sd.delete("taskSchedule", "task_id=?", new String[]{String.valueOf(1)});
sd.close();
}
/**
* 条件查询
*
* @creattime 2012-3-13
*
*/
public void conditionsCheck(){
sd = dboh.getReadableDatabase();
// 查询task_id 为 2 的task_name的值
Cursor cursor = sd.query("taskSchedule", new String[]{"task_id","task_name"}, "task_id=?", new String[]{String.valueOf(2)}, null, null, null);
while(cursor.moveToNext()){
String task_name = cursor.getString(cursor.getColumnIndex("task_name"));
dbinfo.setText(task_name);
}
}
}
3.不要忘记添加权限
<uses-permission android:name="android.permission.ACCESS_CHECKIN_PROPERTIES"/>
编辑器加载中...