Android SQLite数据库的添、删、查、改(2)

来源:互联网 时间:1970-01-01

首先创建一个类MyOpenHelper,继承自SQLiteOpenHelper,用来打开或创建一个数据库

MyOpenHelper
package com.SQLiteTest3;

import android.content.Context;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteDatabase.CursorFactory;import android.database.sqlite.SQLiteOpenHelper;

public class MyOpenHelper extends SQLiteOpenHelper {
String sql = "create table if not exists TestUsers"+
"(id int primary key,name varchar,sex varchar)";

public MyOpenHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(sql);

}

@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub

}

}

然后是实现代码:

package com.SQLiteTest3;

import android.app.Activity;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.*;

public class SQLiteTest3 extends Activity {
Button btnInsert;
Button btnDelete;
Button btnUpdate;
Button btnSelect;
EditText etName;
EditText etSex;
TextView tvShowContent;
MyOpenHelper OpenHelper;
SQLiteDatabase db = null;
public static final String DB_NAME = "DBTest";

View.OnClickListener btnInsertListener = new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
InsertTb();
}
};
View.OnClickListener btnDeleteListener = new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
DeleteTb();
}
};
View.OnClickListener btnUpdateListener = new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
UpdateTb();
}
};
View.OnClickListener btnSelectListener = new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Select();
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
OpenHelper = new MyOpenHelper(this, DB_NAME, null, 1);
btnInsert = (Button) findViewById(R.id.main_btn_insert);
btnDelete = (Button) findViewById(R.id.main_btn_delete);
btnUpdate = (Button) findViewById(R.id.main_btn_update);
btnSelect = (Button) findViewById(R.id.main_btn_select);
tvShowContent = (TextView) findViewById(R.id.main_tv_showContent);
etName = (EditText) findViewById(R.id.main_et_name);
etSex = (EditText) findViewById(R.id.main_et_sex);
btnInsert.setOnClickListener(btnInsertListener);
btnDelete.setOnClickListener(btnDeleteListener);
btnUpdate.setOnClickListener(btnUpdateListener);
btnSelect.setOnClickListener(btnSelectListener);
}

public void InsertTb(){
int flag = -1;
db = OpenHelper.getWritableDatabase();
String strName = etName.getText().toString();
String strSex = etSex.getText().toString();
String sql = "insert into TestUsers (name,sex) values ('"+strName+"','"+strSex+"')";
try {
db.execSQL(sql);

} catch (SQLException e) {
Log.i("err", "insert failed");
flag = 0;
Toast.makeText(SQLiteTest3.this, "插入失败!", Toast.LENGTH_SHORT).show();
}
db.close();
if (flag == -1){
Toast.makeText(SQLiteTest3.this, "插入成功!", Toast.LENGTH_SHORT).show();
}

}
public void DeleteTb(){
int flag = -1;
db = OpenHelper.getWritableDatabase();
String sql = "delete from TestUsers where id = 2";
try {
db.execSQL(sql);
} catch (SQLException e) {
Log.i("err", "delete failed");
flag = 0;
Toast.makeText(SQLiteTest3.this, "删除失败!", Toast.LENGTH_SHORT).show();
}
db.close();
if (flag == -1){
Toast.makeText(SQLiteTest3.this, "删除成功!", Toast.LENGTH_SHORT).show();
}
}
public void UpdateTb() {
int flag = -1;
db = OpenHelper.getWritableDatabase();
String Name = etName.getText().toString();
String sql = "Update TestUsers set name = 'anhong',sex = 'men' where name = '"+Name+"'";
try {
db.execSQL(sql);
} catch (SQLException e) {
Log.i("err", "update failed");
flag = 0;
Toast.makeText(SQLiteTest3.this, "更新失败!", Toast.LENGTH_SHORT).show();
}
db.close();
if (flag == -1){
Toast.makeText(SQLiteTest3.this, "更新成功!", Toast.LENGTH_SHORT).show();
}
}
public void Select(){
db = OpenHelper.getReadableDatabase();
String sql = "select sex from TestUsers where name=?";
Cursor cursor = db.rawQuery(sql, new String[]
{ etName.getText().toString() });
int count = cursor.getCount();
String [] Sex = new String[count];
int i = 0;
if (cursor.getCount() > 0)
{
// 必须使用moveToFirst方法将记录指针移动到第1条记录的位置
/* cursor.moveToFirst(); //取第一条记录
String result = cursor.getString(cursor.getColumnIndex("sex"));
tvShowContent.setText(result); */
//取多条记录
int sexIndex = cursor.getColumnIndex("sex");
for(cursor.moveToFirst();!(cursor.isAfterLast());cursor.moveToNext()){
Sex[i] = cursor.getString(sexIndex);
i++;
}
}
for(int j = 0; j < count; j++){
tvShowContent.append(" ");
tvShowContent.append(Sex[j]);
}
}



}

main.xml

main.xml
<?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"
>
<Button
android:text="@string/add"
android:id="@+id/main_btn_insert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<Button
android:text="@string/delete"
android:id="@+id/main_btn_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<Button
android:text="@string/select"
android:id="@+id/main_btn_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<Button
android:text="@string/update"
android:id="@+id/main_btn_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<TextView
android:text="Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<EditText
android:id="@+id/main_et_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></EditText>
<TextView
android:text="Sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<EditText
android:id="@+id/main_et_sex"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></EditText>
<TextView
android:id="@+id/main_tv_showContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>

</LinearLayout>

主窗体运行截图


相关阅读:
Top