SQLiteOpenHelper学习笔记
2016-07-10 10:40
477 查看
<?xml version="1.0" encoding="utf-8"?> <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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.user.android2_lesson5_sqlitedatabasehelper.MainActivity"> <Button android:id="@+id/create" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="创建表"/> <Button android:id="@+id/insert" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="添加数据"/> <Button android:id="@+id/delete" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="删除数据"/> <Button android:id="@+id/updata" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="修改数据"/> <Button android:id="@+id/select" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="查询数据"/> </LinearLayout> 创建一个datahelper继承SQLiteOpenHelper package com.user.android2_lesson5_sqlitedatabasehelper; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; /** * Created by user on 2016/7/8. */ public class MyDataHelper extends SQLiteOpenHelper { // 构造方法 这个方法必须要有 // public MyDataHelper(Context context,String name ,SQLiteDatabase.CursorFactory factory,int version){ super(context,name,factory,version); } // 当第一次创建数据库的时候会调用该方法 @Override public void onCreate(SQLiteDatabase sqLiteDatabase) { Log.i("DataHelper","创建数据库,初始化数据库"); // 因为是第一次数据库创建的时候调用的,所以这里通常进行的是数据库表的创建 String sql = "CREATE TABLE IF NOT EXISTS student(sid int,name text,sex text,age int)"; // 执行表创建语句 sqLiteDatabase.execSQL(sql); } // 当更新数据库(数据库版本更新)的时候调用该方法 @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { Log.i("DataHelper","更新数据库"); } } package com.user.android2_lesson5_sqlitedatabasehelper; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { private Button create; private Button insert; private Button delete; private Button updata; private Button select; private SQLiteDatabase db; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); create = (Button) findViewById(R.id.create); insert = (Button) findViewById(R.id.insert); delete = (Button) findViewById(R.id.delete); updata = (Button) findViewById(R.id.updata); select = (Button) findViewById(R.id.select); create.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // 创建Helper对象 MyDataHelper helper = new MyDataHelper(MainActivity.this,"student_data2",null,1); // 获取一个可读的数据库对象 db = helper.getReadableDatabase(); } }); // 插入数据 insert.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // 这里注意处理好版本号 MyDataHelper helper = new MyDataHelper(MainActivity.this,"student_data2",null,2); // 得到一个可写入的数据库 db = helper.getWritableDatabase(); // // ContentValues con = new ContentValues(); // con.put("sid",1); // con.put("name","张三"); // con.put("sex","男"); // con.put("age",18); // db.insert("student",null,con); // 或者这样写 String sql = "INSERT INTO student(sid,name,sex,age) VALUES(2,'李三炮','人妖',99); "; db.execSQL(sql); // 关闭数据库 Log.i("123","321"); db.close(); } }); delete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String sql = "DELETE FROM student where sid = 1;"; db.execSQL(sql); Log.i("1","删除成功"); } }); updata.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { MyDataHelper helper = new MyDataHelper(MainActivity.this,"student_data2",null,2); db = helper.getReadableDatabase(); } }); select.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Cursor cursor = db.query("student",null,null,null,null,null,null); Log.i("2","ssss"); if (cursor.moveToFirst()){ do { int sid = cursor.getInt(0); String name = cursor.getString(1); String sex = cursor.getString(2); int age = cursor.getInt(3); }while(cursor.moveToNext()); } } }); } }
相关文章推荐
- SQLiteOpenHelper(转)
- PL/SQL Developer创建Job的步骤
- MySQL Optimizer Tracemy 与 logmnr:MySQL binlog logmnr----MYSQL 邱伟胜 专家博客
- SQLiteOpenHelper以及ContentProvite
- 详解 Spotlight on MySQL监控MySQL服务器
- SQL SERVER 2008如何建立自动备份的维护计划
- PL/SQL Developer去掉启动时自动弹出的Logon弹出框方法
- PL/SQL Developer去掉启动时自动弹出的Logon弹出框方法
- sql:除非另外还指定了 TOP 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询
- MySQL复制中slave延迟监控
- SQL Server中TOP子句可能导致的问题以及解决办法
- MySQL架构优化:定时计划任务与表分区
- mysql数据库高可用架构-MHA-0.56/0.57详解
- MySQL 企业常用架构与调优经验分享
- PL/SQL Developer 免安装步骤
- ibatis/mybatis显示sql语句 log4j.properties配置文件
- win7_oracle11g_64位连接32位PLSQL_Developer
- mysql 查询copy to tmp table造成堵塞
- sql service 触发器,监控一个表有新数据插入时,把这条数据再插到另一个表
- 恢复oracle中用pl/sql误删除drop掉的表