1.创建一个SQLiteOpenHelper数据库
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class MySqLiteOpenDatabase extends SQLiteOpenHelper {
public MySqLiteOpenDatabase(Context context) {
super(context, "day03", null, 1);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
String sqi="create table student(_id integer primary key autoincrement,userName text,userAge text)";
db.execSQL(sqi);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener{
private EditText age;
private EditText name;
private Button add;
private Button delect;
private Button motify;
private Button query;
private MySqLiteOpenDatabase mysqllite;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
age = (EditText) findViewById(R.id.age);
name = (EditText) findViewById(R.id.name);
add = (Button) findViewById(R.id.add);
delect = (Button) findViewById(R.id.delect);
motify = (Button) findViewById(R.id.motify);
query = (Button) findViewById(R.id.query);
add.setOnClickListener(this);
delect.setOnClickListener(this);
motify.setOnClickListener(this);
query.setOnClickListener(this);
mysqllite = new MySqLiteOpenDatabase(this);
//打开数据库
SQLiteDatabase database = mysqllite.getReadableDatabase();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.add:
String name1 = name.getText().toString().trim();
String age1 = age.getText().toString().trim();
//打开数据库
SQLiteDatabase database = mysqllite.getReadableDatabase();
ContentValues values=new ContentValues();
values.put("userName", name1);
values.put("userAge", age1);
//添加数据
database.insert("student", null, values);
database.close();
break;
case R.id.delect://删除
String name2 = name.getText().toString().trim();
SQLiteDatabase readableDatabase = mysqllite.getReadableDatabase();
readableDatabase.delete("student", "userName=?", new String[]{name2});
readableDatabase.close();
break;
case R.id.motify://修改
String name3 = name.getText().toString().trim();
String age3 = age.getText().toString().trim();
SQLiteDatabase database2 = mysqllite.getReadableDatabase();
ContentValues values2 = new ContentValues();
values2.put("userName", name3);
values2.put("userAge", age3);
database2.update("student", values2, "userName=?", new String[]{name3});
database2.close();
break;
case R.id.query://查询
String name4 = name.getText().toString().trim();
SQLiteDatabase database3 = mysqllite.getReadableDatabase();
Cursor cursor = database3.query( "student", new String[]{"userName"}, "userName=?", new String[]{name4}, null, null, null, null);
if(cursor!=null&&cursor.getCount()>0){
while(cursor.moveToNext()){
String string = cursor.getString(cursor.getColumnIndex("userName"));
System.out.println("名字==="+string);
}
}
database3.close();
break;
default:
break;
}
}
}