SQLite数据库存储之更新数据库
来源:互联网 发布:ubuntu 进入根目录 编辑:程序博客网 时间:2024/05/16 04:54
本文根据郭霖的《第一行代码》摘录总结
四.更新数据库
同样在SQLiteDatabase()中也提供了一个非常好用的update()方法,这个方法接收四个参数,第一个依然是表名,第二个是ContentValues对象,第三个,第四个参数用于去约束更新某一行或者某几行的数据,不指定的话就是默认更新所有行。
修改activity_main.xml中的代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.asus.sqlitetest.MainActivity"> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="保存" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="增加" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="更新" /></LinearLayout>
然后修改MainActivity中的代码:
public class MainActivity extends AppCompatActivity { private MyDatabaseHelper dbhelper; private Button button3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dbhelper=new MyDatabaseHelper(this,"BookStore.db",null,2); button3=(Button)findViewById(R.id.button2); button3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SQLiteDatabase db=dbhelper.getWritableDatabase(); ContentValues values=new ContentValues(); values.put("price",888); db.update("book",values,"name=?",new String[]{"xiha"}); } }); }}
可以看到,这里使用了第三个,第四个参数来指定具体的行,而?是一个占位符,可以通过第四个参数提供的一个字符串数组为第三个参数中的每个占位符指定相应的内容。
1 0
- SQLite数据库存储之更新数据库
- Android存储之SQLite数据库
- android存储之SQLite数据库
- SQLite数据库存储之创建数据库
- SQLite数据库存储之创建数据库
- SQLite数据库存储之升级数据库
- Android数据存储之SQLite数据库存储
- Android数据存储之SQLite数据库存储
- Android数据存储之SQLite数据库存储
- Android数据存储之:SQLite数据库存储
- 数据存储之SQLite数据库存储
- 文件存储之SQLite数据库存储
- Android 数据存储之SQLite数据库存储
- Android数据存储之SQLite数据库存储
- Android 数据存储之 SQLite嵌入式数据库
- 数据存储之三SQLite嵌入式数据库
- android学习笔记之SQLite数据库存储
- 数据存储之SQLite数据库操作
- Android csdn大神
- VS2010 WDK7.1.0 Win7_64
- minicom的配置与使用
- “error LNK2019: 无法解析的外部符号”之分析
- java冒泡排序算法
- SQLite数据库存储之更新数据库
- POJ-2318 TOYS,暴力+叉积判断!
- http协议详解
- react native listView(1) 加载网络数据简单版
- 几个常见的 Socket 连接错误及原因[转]
- 常用mysql自带内置函数说明&相似对比
- try catch
- iOS/Objective-C开发 枚举(Enum)的使用
- Java网络编程,通过TCP,Socket实现多对一的局域网聊天室