现在的位置: 首页 > 数据库 > 正文

使用占位符对数据库进行操作

2019年07月20日 数据库 ⁄ 共 3456字 ⁄ 字号 评论关闭

 

 

 

在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);

     }

    

  }

}

 

抱歉!评论已关闭.