博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android系统下读写Sqlite数据库的源码
阅读量:6518 次
发布时间:2019-06-24

本文共 8173 字,大约阅读时间需要 27 分钟。

将开发过程经常用的内容片段做个备份,如下内容是关于Android系统下读写Sqlite数据库的的内容。

Licensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License atUnless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.package com.commonsware.android.constants;import android.content.ContentValues;import android.content.Context;import android.database.Cursor;import android.database.SQLException;import android.database.sqlite.SQLiteOpenHelper;import android.database.sqlite.SQLiteDatabase;import android.hardware.SensorManager;public class DatabaseHelper extends SQLiteOpenHelper {private static final String DATABASE_NAME="db";public static final String TITLE="title";public static final String VALUE="value";public DatabaseHelper(Context context) {super(context, DATABASE_NAME, null, 1);}@Overridepublic void onCreate(SQLiteDatabase db) {db.execSQL("CREATE TABLE constants (_id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, value REAL);");ContentValues cv=new ContentValues();cv.put(TITLE, "Gravity, Death Star I");cv.put(VALUE, SensorManager.GRAVITY_DEATH_STAR_I);db.insert("constants", TITLE, cv);cv.put(TITLE, "Gravity, Earth");cv.put(VALUE, SensorManager.GRAVITY_EARTH);db.insert("constants", TITLE, cv);cv.put(TITLE, "Gravity, Jupiter");cv.put(VALUE, SensorManager.GRAVITY_JUPITER);db.insert("constants", TITLE, cv);cv.put(TITLE, "Gravity, Mars");cv.put(VALUE, SensorManager.GRAVITY_MARS);db.insert("constants", TITLE, cv);cv.put(TITLE, "Gravity, Mercury");cv.put(VALUE, SensorManager.GRAVITY_MERCURY);db.insert("constants", TITLE, cv);cv.put(TITLE, "Gravity, Moon");cv.put(VALUE, SensorManager.GRAVITY_MOON);db.insert("constants", TITLE, cv);cv.put(TITLE, "Gravity, Neptune");cv.put(VALUE, SensorManager.GRAVITY_NEPTUNE);db.insert("constants", TITLE, cv);cv.put(TITLE, "Gravity, Pluto");cv.put(VALUE, SensorManager.GRAVITY_PLUTO);db.insert("constants", TITLE, cv);cv.put(TITLE, "Gravity, Saturn");cv.put(VALUE, SensorManager.GRAVITY_SATURN);db.insert("constants", TITLE, cv);cv.put(TITLE, "Gravity, Sun");cv.put(VALUE, SensorManager.GRAVITY_SUN);db.insert("constants", TITLE, cv);cv.put(TITLE, "Gravity, The Island");cv.put(VALUE, SensorManager.GRAVITY_THE_ISLAND);db.insert("constants", TITLE, cv);cv.put(TITLE, "Gravity, Uranus");cv.put(VALUE, SensorManager.GRAVITY_URANUS);db.insert("constants", TITLE, cv);cv.put(TITLE, "Gravity, Venus");cv.put(VALUE, SensorManager.GRAVITY_VENUS);db.insert("constants", TITLE, cv);}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {android.util.Log.w("Constants", "Upgrading database, which will destroy all old data");db.execSQL("DROP TABLE IF EXISTS constants");onCreate(db);}}复制代码ConstantsBrowser.javaLicensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License atUnless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.package com.commonsware.android.constants;import android.app.AlertDialog;import android.app.ListActivity;import android.content.ContentValues;import android.content.DialogInterface;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.os.Bundle;import android.text.method.NumberKeyListener;import android.view.ContextMenu;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.AdapterView;import android.widget.EditText;import android.widget.ListAdapter;import android.widget.SimpleCursorAdapter;import android.widget.TextView;public class ConstantsBrowser extends ListActivity {private static final int ADD_ID = Menu.FIRST+1;private static final int DELETE_ID = Menu.FIRST+3;private static final int CLOSE_ID = Menu.FIRST+4;private SQLiteDatabase db=null;private Cursor constantsCursor=null;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);db=(new DatabaseHelper(this)).getWritableDatabase();constantsCursor=db.rawQuery("SELECT _ID, title, value "+"FROM constants ORDER BY title",null);ListAdapter adapter=new SimpleCursorAdapter(this,R.layout.row, constantsCursor,new String[] {"title", "value"},new int[] {R.id.title, R.id.value});setListAdapter(adapter);registerForContextMenu(getListView());}@Overridepublic void onDestroy() {super.onDestroy();constantsCursor.close();db.close();}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {menu.add(Menu.NONE, ADD_ID, Menu.NONE, "Add").setIcon(R.drawable.add).setAlphabeticShortcut('a');menu.add(Menu.NONE, CLOSE_ID, Menu.NONE, "Close").setIcon(R.drawable.eject).setAlphabeticShortcut('c');return(super.onCreateOptionsMenu(menu));}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {switch (item.getItemId()) {case ADD_ID:add();return(true);case CLOSE_ID:finish();return(true);}return(super.onOptionsItemSelected(item));}@Overridepublic void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {menu.add(Menu.NONE, DELETE_ID, Menu.NONE, "Delete").setIcon(R.drawable.delete).setAlphabeticShortcut('d');}@Overridepublic boolean onContextItemSelected(MenuItem item) {switch (item.getItemId()) {case DELETE_ID:AdapterView.AdapterContextMenuInfo info=(AdapterView.AdapterContextMenuInfo)item.getMenuInfo();delete(info.id);return(true);}return(super.onOptionsItemSelected(item));}private void add() {LayoutInflater inflater=LayoutInflater.from(this);View addView=inflater.inflate(R.layout.add_edit, null);final DialogWrapper wrapper=new DialogWrapper(addView);new AlertDialog.Builder(this).setTitle(R.string.add_title).setView(addView).setPositiveButton(R.string.ok,new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog,int whichButton) {processAdd(wrapper);}}).setNegativeButton(R.string.cancel,new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog,int whichButton) {}}).show();}private void delete(final long rowId) {if (rowId>0) {new AlertDialog.Builder(this).setTitle(R.string.delete_title).setPositiveButton(R.string.ok,new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog,int whichButton) {processDelete(rowId);}}).setNegativeButton(R.string.cancel,new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog,int whichButton) {}}).show();}}private void processAdd(DialogWrapper wrapper) {ContentValues values=new ContentValues(2);values.put("title", wrapper.getTitle());values.put("value", wrapper.getValue());db.insert("constants", "title", values);constantsCursor.requery();}private void processDelete(long rowId) {String[] args={String.valueOf(rowId)};db.delete("constants", "_ID=?", args);constantsCursor.requery();}class DialogWrapper {EditText titleField=null;EditText valueField=null;View base=null;DialogWrapper(View base) {this.base=base;valueField=(EditText)base.findViewById(R.id.value);}String getTitle() {return(getTitleField().getText().toString());}float getValue() {return(new Float(getValueField().getText().toString()).floatValue());}private EditText getTitleField() {if (titleField==null) {titleField=(EditText)base.findViewById(R.id.title);}return(titleField);}private EditText getValueField() {if (valueField==null) {valueField=(EditText)base.findViewById(R.id.value);}return(valueField);}}}
android:orientation="horizontal"android:layout_width="fill_parent"android:layout_height="fill_parent">
android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="wrap_content">

转载于:https://blog.51cto.com/14311234/2386748

你可能感兴趣的文章
使用eclipse与android studio 在开发自定义控件时的区别
查看>>
我的友情链接
查看>>
mysql学习笔记
查看>>
年年有鱼游戏Android源码项目
查看>>
java使用Iterator、for循环同步数据
查看>>
创建镜像iso文件
查看>>
Linux下创建软RAID5和RAID10实战
查看>>
C++类的存储
查看>>
ActiveReports 报表应用教程 (8)---交互式报表之动态过滤
查看>>
解决使用Handler时Can't create handler inside thread that has not called Looper.prepare()
查看>>
跟我一起学docker(四)--容器的基本操作
查看>>
磁化强度
查看>>
C/C++ 数据范围
查看>>
LVS+keepalived+nginx
查看>>
monkey如何通过uiautomatorviewer的bounds坐标点击控件
查看>>
第22章,mysql数据库-1
查看>>
【亲测】教你如何搭建 MongoDB 复制集 + 选举原理
查看>>
虚拟化网络技术
查看>>
阿里云中间件推出全新开发者服务
查看>>
56.随机产生的id重复问题
查看>>