前言
欢迎大家我分享和推荐好用的代码段~~
声明
欢迎转载,但请保留文章原始出处:
CSDN:http://www.csdn.net
雨季o莫忧离:http://blog.csdn.net/luckkof
正文
首先创建一个类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;
publicclass 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
publicvoid onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(sql);
}
@Override
publicvoid 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.*;
publicclass SQLiteTest3 extends Activity {
Button btnInsert;
Button btnDelete;
Button btnUpdate;
Button btnSelect;
EditText etName;
EditText etSex;
TextView tvShowContent;
MyOpenHelper OpenHelper;
SQLiteDatabase db =null;
publicstaticfinal String DB_NAME ="DBTest";
View.OnClickListener btnInsertListener =new View.OnClickListener() {
@Override
publicvoid onClick(View v) {
// TODO Auto-generated method stub
InsertTb();
}
};
View.OnClickListener btnDeleteListener =new View.OnClickListener() {
@Override
publicvoid onClick(View v) {
// TODO Auto-generated method stub
DeleteTb();
}
};
View.OnClickListener btnUpdateListener =new View.OnClickListener() {
@Override
publicvoid onClick(View v) {
// TODO Auto-generated method stub
UpdateTb();
}
};
View.OnClickListener btnSelectListener =new View.OnClickListener() {
@Override
publicvoid onClick(View v) {
// TODO Auto-generated method stub
Select();
}
};
/** Called when the activity is first created. */
@Override
publicvoid 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);
}
publicvoid 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();
}
}
publicvoid 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();
}
}
publicvoid 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();
}
}
publicvoid Select(){
db = OpenHelper.getReadableDatabase();
String sql ="select sex from TestUsers where name=?";
Cursor cursor = db.rawQuery(sql, new String[]
{ etName.getText().toString() }); //String sql = "select sex from TestUsers"; //Cursor cursor = db.rawQuery(sql, null);
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>
主窗体运行截图