android SQLite的CRUD

本文通过一个Android应用示例,展示了如何使用SQLite进行数据库的基本操作,包括创建数据库、插入数据、更新数据、读取数据和删除数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

activity12.java

package com.wansha; import android.app.Activity; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import com.wansha.db.DatabaseHelper; public class Activity12 extends Activity { private Button createDatabase; private Button updateDatabase; private Button insert; private Button update; private Button read; private Button delete; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); this.createDatabase = (Button)this.findViewById(R.id.createDatabase); this.createDatabase.setOnClickListener(new CreateDatabaseListener()); this.updateDatabase = (Button)this.findViewById(R.id.updateDatabase); this.updateDatabase.setOnClickListener(new UpdateDatabaseListener()); this.insert = (Button)this.findViewById(R.id.insert); this.insert.setOnClickListener(new InsertListener()); this.update = (Button)this.findViewById(R.id.update); this.update.setOnClickListener(new UpdateListener()); this.read = (Button)this.findViewById(R.id.read); this.read.setOnClickListener(new ReadListener()); this.delete = (Button)this.findViewById(R.id.delete); this.delete.setOnClickListener(new DeleteListener()); } class CreateDatabaseListener implements OnClickListener{ @Override public void onClick(View v) { SQLiteOpenHelper sqlite = new DatabaseHelper(Activity12.this,"sharp"); sqlite.getReadableDatabase(); } } class UpdateDatabaseListener implements OnClickListener{ @Override public void onClick(View v) { System.out.println("UpdateDatabaseListener is invoked !!!"); SQLiteOpenHelper sqlite = new DatabaseHelper(Activity12.this,"sharp",2); sqlite.getReadableDatabase(); } } class InsertListener implements OnClickListener{ @Override public void onClick(View v) { SQLiteOpenHelper sqlite = new DatabaseHelper(Activity12.this,"sharp"); SQLiteDatabase database = sqlite.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("username", "xiaopeng"); database.insert("sharp", null, values); } } class UpdateListener implements OnClickListener{ @Override public void onClick(View v) { SQLiteOpenHelper sqlite = new DatabaseHelper(Activity12.this,"sharp"); SQLiteDatabase database = sqlite.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("username", "pengpeng"); database.update("sharp", values, "id=?", new String[]{"2"}); } } class ReadListener implements OnClickListener{ @Override public void onClick(View v) { SQLiteOpenHelper sqlite = new DatabaseHelper(Activity12.this,"sharp"); SQLiteDatabase database = sqlite.getWritableDatabase(); Cursor cursor = database.query("sharp", new String[]{"id","username"}, null, null, null, null, null); while(cursor.moveToNext()){ String username = cursor.getString(cursor.getColumnIndex("username")); System.out.println("username---->" + username); } } } class DeleteListener implements OnClickListener{ @Override public void onClick(View v) { SQLiteOpenHelper sqlite = new DatabaseHelper(Activity12.this,"sharp"); SQLiteDatabase database = sqlite.getWritableDatabase(); database.delete("sharp", "id=?", new String[]{"1"}); } } }
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" > <Button android:id="@+id/createDatabase" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="创建数据库" /> <Button android:id="@+id/updateDatabase" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="更新数据库" /> <Button android:id="@+id/insert" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="插入数据" /> <Button android:id="@+id/update" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="更新数据" /> <Button android:id="@+id/read" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="读取数据" /> <Button android:id="@+id/delete" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="删除数据" /> </LinearLayout>

db.execSQL("create table sharp(id integer primary key autoincrement, username varchar(255))");

创建自增长ID


DatebaseHelper.java

package com.wansha.db; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; public class DatabaseHelper extends SQLiteOpenHelper{ private static final int VERSION = 1; public DatabaseHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); } public DatabaseHelper(Context context, String name,int version) { this(context, name, null, version); } public DatabaseHelper(Context context, String name) { this(context, name, VERSION); } @Override public void onCreate(SQLiteDatabase db) { System.out.println("create table is invoked !!!"); db.execSQL("create table sharp(id integer primary key autoincrement, username varchar(255))"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { System.out.println("update table is invoked !!!"); } }



大家好,个人觉得用Sqlite数据库时,经常需要进行机械性的CRUD操作,故对其进行了一下封装,希望能起到抛砖引玉的作用。 目的:封装共有的CRUD 下面简单的说一下使用步骤,如果觉得多余,可以无视。 1. 实现自己的DBHelper: /** * * @author Kee.Li * * 1. 继承了SmartDBHelper,不需要重写SQLiteOpenHelper的那两个方法 * 2. 父类构造方法参数modelClasses是实体类的数组,也就是需要生产表的类的Class数组 * */ public class DBHelper extends SmartDBHelper { //数据库名称 private final static String DATABASE_NAME = "books.db"; //数据库版本 private final static int DATABASE_VERSION = 2; //需要生成数据库表的类的数组 private final static Class<?>[] modelClasses = {Book.class,User.class}; public DBHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION,modelClasses); } } 2.创建app需要的实体,也就是对应的数据库表(这里的实体添加到DBHelper的modelClasses数组中) /** * 数据库的实体 * @author Kee.Li * 关于注解: * Table: 此类对应的数据库表名 * Id:标识此属性为数据库自增长的id,应为int型 * Column:标识此属性对应的数据库字段名 */ @Table(name="t_books") public class Book{ @Id @Column(name="book_id") private int bookId; @Column(name="book_name") private String bookName; @Column(name="book_author") private String bookAuthor; //set get 方法省略.... } 3. 实现DAO,也就是对实体的CRUD类 /** * @author Kee.Li * * 此类只需要继承TemplateDAO,在构造方法里面给父类的属性dbHelper赋值,即可实现CRUD操作 * 若有复杂的操作,可以自定义方法 */ public class BookDAO extends TemplateDAO { /** * 创建DAO时初始化连接数据库对象helper * @param context */ public BookDAO(Context context) { super(new DBHelper(context)); } } 4. activity的调用 bookDAO = new BookDAO(this); List books = bookDAO.find(); 好了,到此结束,如果有什么好的建议或者意见,希望可以共同学习!谢谢大家!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值