Android SQLite API的使用(非原创)
1.使用SQLite的API来进行数据库的添加、删除、修改、查询
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | package com.example.sqlitedatabase.test; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.test.AndroidTestCase; import com.example.sqlitedatabase.MyOpenHelper; public class JunitTest2 extends AndroidTestCase{ private MyOpenHelper helper; private SQLiteDatabase db; @Override protected void setUp() throws Exception { // TODO Auto-generated method stub super .setUp(); helper = new MyOpenHelper(getContext(), "emp.db" , null , 1 ); db = helper.getWritableDatabase(); } @Override protected void tearDown() throws Exception { // TODO Auto-generated method stub super .tearDown(); db.close(); } public void test() { } public void insertAction() { db.execSQL( "insert into Emp(name,salary) values('张无忌','12000')" ); db.execSQL( "insert into Emp(name,salary) values('赵敏','11000')" ); db.execSQL( "insert into Emp(name,salary) values('谢逊','16000')" ); } public void deleteAction() { db.execSQL( "delete from Emp where name = '赵敏'" ); } public void updateAction() { db.execSQL( "update Emp set salary = '18000' where name = ?" , new Object[] { "谢逊" }); } public void selectAction() { Cursor c = db.rawQuery( "select * from Emp" , null ); while (c.moveToNext()) { String id = c.getString(c.getColumnIndex( "id" )); String name = c.getString(c.getColumnIndex( "name" )); String salary = c.getString(c.getColumnIndex( "salary" )); System.out.println(id + " , " + name + " , " + salary); } } public void insertAPI() { //添加 ContentValues values = new ContentValues(); //相当于map //添加的时候key一定要是Emp表中存在的字段 values.put( "name" , "洪七公" ); values.put( "salary" , "5000" ); //insert(String table, String nullColumnHack, ContentValues values) db.insert( "Emp" , null , values); } public void deleteAPI() { //删除 /* * delete(String table, String whereClause, String[] whereArgs) * whereClause * the optional WHERE clause to apply when deleting. Passing null will delete all rows. * whereArgs * You may include ?s in the where clause, which will be replaced by the values from whereArgs. The values will be bound as Strings. */ int columns = db.delete( "Emp" , "name = ?" , new String[] { "谢逊" }); System.out.println( "行数:" + columns); } public void updateAPI() { //修改 ContentValues values = new ContentValues(); values.put( "salary" , 500 ); //update(String table, ContentValues values, String whereClause, String[] whereArgs) int columns = db.update( "Emp" , values, "name = ?" , new String[] { "张无忌" }); System.out.println( "行数:" + columns); } public void selectAPI() { //查询 //query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy) Cursor c = db.query( "Emp" , new String[] { "name" , "salary" }, " id > ?" , new String[] { "1" }, null , null , null ); System.out.println( "ID大于1的人有:" + c.getCount() + "个人" ); while (c.moveToNext()) { String name = c.getString(c.getColumnIndex( "name" )); //获取name字段对应的下标 String salary = c.getString(c.getColumnIndex( "salary" )); System.out.println(name + " , " + salary); } } public void transaction() { try { db.beginTransaction(); // 开始事务 ContentValues values = new ContentValues(); values.put( "salary" , 300 ); db.update( "Emp" , values, "name = ?" , new String[] { "张无忌" }); values.clear(); int r = 4 / 0 ; // 模拟错误 values.put( "salary" , 5200 ); db.update( "Emp" , values, "name = ?" , new String[] { "洪七公" }); db.setTransactionSuccessful(); } catch (Exception e) { System.out.println( "事务回滚啦" ); } finally { db.endTransaction(); //结束事务的同时会提交 } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器