您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關如何使用占位符對數據庫進行操作,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
效果圖
在main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center_horizontal">
<Button
android:id="@+id/insertBut"
android:layout_marginTop="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="增加數據" />
<Button
android:id="@+id/updateBut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改數據" />
<Button
android:id="@+id/deleteBut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="刪除數據" />
</LinearLayout>
創建數據庫的建表操作類Mytab.java
package com.li.sqlite;
import android.database.sqlite.SQLiteDatabase;
public class Mytab {
private static final String TABLENAME = "mytab"; // 表示要操作的數據表名稱
private SQLiteDatabase db = null; // 數據庫操作
public Mytab(SQLiteDatabase db) {
this.db = db;
}
public void insert(String name,String birthday) { //向表中增加數據
String sql = "INSERT INTO " + TABLENAME + "(name,birthday) VALUES (?,?)";
Object args[] = new Object[]{name,birthday};
this.db.execSQL(sql,args) ;
this.db.close() ;
}
public void update(int id, String name, String birthday) { //修改表的數據
String sql = "UPDATE " + TABLENAME + " SET name=?,birthday=? WHERE id=?";
Object args[] = new Object[]{name,birthday,id};
this.db.execSQL(sql,args);
this.db.close() ;
}
public void delete(int id) { //刪除表的數據
String sql = "DELETE FROM " + TABLENAME + " WHERE id=?";
Object args[] = new Object[]{id};
this.db.execSQL(sql,args) ;
this.db.close() ;
}
}
在MySQLiteDemo.java中:
package com.li.sqlite;
import android.app.Activity;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MySQLiteDemo extends Activity {
private Button inserBut = null;
private Button updateBut = null;
private Button deleteBut = null;
private SQLiteOpenHelper helper = null;
private Mytab mtab = null;
private static int count = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.inserBut = (Button)super.findViewById(R.id.insertBut);
this.updateBut = (Button)super.findViewById(R.id.updateBut);
this.deleteBut = (Button)super.findViewById(R.id.deleteBut);
this.helper = new MyDatabaseHelper(this);
this.inserBut.setOnClickListener(new InertOnClickListenerImpl());
this.updateBut.setOnClickListener(new UpdateOnClickListenerImpl());
this.deleteBut.setOnClickListener(new DeleteOnClickListenerImpl());
}
private class InertOnClickListenerImpl implements OnClickListener{
public void onClick(View v) {
MySQLiteDemo.this.mtab = new Mytab(
MySQLiteDemo.this.helper.getWritableDatabase());
MySQLiteDemo.this.mtab.insert("liyewen" + count++, "1988-08-16");
}
}
private class UpdateOnClickListenerImpl implements OnClickListener{
public void onClick(View v) {
MySQLiteDemo.this.mtab = new Mytab(
MySQLiteDemo.this.helper.getWritableDatabase());
MySQLiteDemo.this.mtab.update(30, "update", "1988/8/15");
}
}
private class DeleteOnClickListenerImpl implements OnClickListener{
public void onClick(View v) {
MySQLiteDemo.this.mtab = new Mytab(
MySQLiteDemo.this.helper.getWritableDatabase());
MySQLiteDemo.this.mtab.delete(31);
}
}
}
關于“如何使用占位符對數據庫進行操作”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。