Android(安卓)SQLite数据操作 【学习记录】
16lz
2021-12-18
main.xml文件:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/create" android:text="创建数据库" /><Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/upgrade" android:text="更新数据库" /><Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/insert" android:text="插入数据" /><Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/update" android:text="修改数据" /><Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/select" android:text="查询数据" /></LinearLayout>
继承自SQLiteOpenHelper的MyOpenHleper类:
package com.android.danny.hleper;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;public class MyOpenHleper extends SQLiteOpenHelper {public static final int VERSION =1;public MyOpenHleper(Context context, String name) {super(context, name, null, VERSION);// TODO Auto-generated constructor stub}public MyOpenHleper(Context context, String name,int ver) {super(context, name, null, ver);// TODO Auto-generated constructor stub}@Overridepublic void onCreate(SQLiteDatabase db) {System.out.println("------Create Database----");db.execSQL("CREATE TABLE person (id int ,name varchar(20))");}@Overridepublic void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {// TODO Auto-generated method stubSystem.out.println("------Upgrade Database----");}}
SQLite数据操作类:
package com.android.danny.db;import com.android.danny.hleper.MyOpenHleper;import android.app.Activity;import android.content.ContentValues;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class Sqlite3 extends Activity implements OnClickListener{ Button create;Button upgrade;Button insert;Button update;Button select; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); create = (Button)findViewById(R.id.create); upgrade = (Button)findViewById(R.id.upgrade); insert = (Button)findViewById(R.id.insert); update = (Button)findViewById(R.id.update); select = (Button)findViewById(R.id.select); create.setOnClickListener(this); upgrade.setOnClickListener(this); insert.setOnClickListener(this); update.setOnClickListener(this); select.setOnClickListener(this); }@Overridepublic void onClick(View v) {MyOpenHleper dbDatabase;if (v == create) {dbDatabase = new MyOpenHleper(this, "test_db.db");//创建数据库dbDatabase.getWritableDatabase();}else if (v == upgrade) {//更新数据库dbDatabase = new MyOpenHleper(this, "test_db.db",2);dbDatabase.getReadableDatabase();}else if (v == insert) {//插入数据ContentValues values = new ContentValues();values.put("id", 1);values.put("name", "danny");dbDatabase = new MyOpenHleper(this, "test_db.db");SQLiteDatabase database = dbDatabase.getWritableDatabase();database.insert("person", null, values);}else if (v == update) {//修改数据ContentValues values = new ContentValues();values.put("name", "danny&kiki");dbDatabase = new MyOpenHleper(this, "test_db.db");SQLiteDatabase database = dbDatabase.getWritableDatabase();database.update("person", values, "id=?", new String[] {"1"});}else if (v == select) {//数据查询操作dbDatabase = new MyOpenHleper(this, "test_db.db");SQLiteDatabase database = dbDatabase.getReadableDatabase();Cursor cursor = database.query("person", new String[] {"id","name"},"id=?", new String[] {"1"}, null, null, null);while (cursor.moveToNext()) {String name = cursor.getString(cursor.getColumnIndex("name"));System.out.println("query:----->"+name);}}}}
更多相关文章
- Android解析json数据
- Android创建文件夹及文件并写入数据
- android创建数据库和表
- android sqlite3查询
- Android(安卓)ORM框架 GreenDao 的使用详解
- 每日一道Android(安卓)面试题,面试途中不败题
- 如何向android程序添加音效或者音频文件
- Android滚动加载数据
- mybatisplus的坑 insert标签insert into select无参数问题的解决