安卓的sqlite增删改
2016-05-22 00:21
453 查看
基于安卓的sqlite增删改,笔记学习:1、使用LinearLayout 布局生成,增删改的页面如图代码布局如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <Button android:onClick="click_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="添加" /> <Button android:onClick="click_delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="删除" /> <Button android:onClick="click_update" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="修改" /> <Button android:onClick="click_search" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="查询" /> </LinearLayout>后台代码:
private MySqliteHelper helper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); helper = new MySqliteHelper(getApplicationContext()); //打开或者创建数据库 第一次 SQLiteDatabase data=helper.getWritableDatabase(); //打开或者创建数据库 第一次 磁盘满了就返回只读数据库 //SQLiteDatabase data=helper.getReadableDatabase(); } //新增 public void click_add(View v){ SQLiteDatabase db=helper.getWritableDatabase(); for (int i = 0; i < 10; i++) { db.execSQL("INSERT into table_info(name,pwd) values('用户"+i+"','1')"); } Toast.makeText(MainActivity.this, "创建完成1000条记录", 0); } //修改 public void click_update(View v){ SQLiteDatabase db=helper.getWritableDatabase(); db.execSQL("update table_info set pwd='0000'"); }2、MySqliteHelper类说明
public class MySqliteHelper extends SQLiteOpenHelper { //自定义访问sqlite public MySqliteHelper(Context context) { super(context, "CarDb.db", null, 3); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub System.out.print("开始创建数据库.."); db.execSQL("create table table_info (_id integer primary key autoincrement,name varchar(20),pwd varchar(50))"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub db.execSQL("alter table table_info add status varchar(2)"); }学习笔记记录,点击按钮往数据库添加和修改数据。
相关文章推荐
- oracle linux 5.9 不支持 acfs 文件系统 怎么解决呢?
- MySQL安装配置方法教程
- MySql 中文乱码
- MySQL - undefined function mysql_connect()
- MySQL - ERROR 1054 (42S22)
- MySQL - ERROR 1062 (23000)
- MySQL - ERROR 1235 (42000)
- MySQL - ERROR 1839
- MySQL - Found option without preceding group in config file
- MySQL 5.7启动失败
- 使用hibernate连接mysql数据库正向工程hibernate.cfg.xml文件和xxx.hbm.xml配置文件
- sql必知必会
- hibernate正向工程连接Oracle数据库的配置文件
- cobol学习之十数据库的增删改查模板
- MySQL优化必须调整的10项配置
- mysql多种存储引擎
- ORACLE 10.2.0.1 ,10gr2 rac 搭建步骤详细
- Mysql InnoDB与MyISAM
- 数据库定时备份原理,代码
- window mysql 5.7 解压版 安装过程详解