数据库实现增删改查
程序员文章站
2022-05-15 23:44:03
...
布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="添加"
android:onClick="add"
/>
<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查询"
android:onClick="select"
/>
<Button
android:id="@+id/btn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="删除"
android:onClick="delete"
/>
<Button
android:id="@+id/btn4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="修改"
android:onClick="update"
/>
</LinearLayout>
package com.example.nshujuk3;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
UerDao uerdao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uerdao = new UerDao(this);
}
public void add(View v){
uerdao.add("张","20");
}
public void delete(View v){
uerdao.delete("张");
}
public void update(View v){
uerdao.update("霍","20","张");
}
public void select(View v){
String name = uerdao.select();
Toast.makeText(MainActivity.this,name,Toast.LENGTH_SHORT).show();
}
}
package com.example.nshujuk3;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class Sql extends SQLiteOpenHelper{
public Sql(Context context) {
super(context,"User-db",null,1);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table user(id integer primary key autoincrement,"+"name text,"+"age text)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
package com.example.nshujuk3;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.view.View;
public class UerDao {
Sql sql;
SQLiteDatabase database;
public UerDao(Context context){
sql = new Sql(context);
database = sql.getReadableDatabase();
}
public void add(String string, String string2) {
// TODO Auto-generated method stub
database.execSQL("insert into user (name,age) values(?,?)",
new Object[]{string,string2}
);
}
public void delete(String string) {
// TODO Auto-generated method stub
database.execSQL("delete from user where name = ?",
new Object[]{string}
);
}
public void update(String string, String string2, String string3) {
// TODO Auto-generated method stub
database.execSQL("update user set name=?,age=? where name=?",
new Object[]{string,string2,string3}
);
}
public String select() {
// TODO Auto-generated method stub
Cursor cursor = database.rawQuery("select * from user",null);
StringBuffer buffer = new StringBuffer();
while(cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex("name"));
String age = cursor.getString(cursor.getColumnIndex("age"));
buffer.append(name+age);
}
return buffer.toString();
}
}