Android之sqlite操作
来源:互联网 发布:html5引导页源码 编辑:程序博客网 时间:2024/05/22 07:36
Android使用sqlite数据库不需要安装。
先创建一个名为DBTest 的项目:
AndroidMainfest.xml的代码如下:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.dbtest" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.dbtest.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
DBUtil的 代码如下:
package com.example.dbtest;import java.util.ArrayList;import java.util.List;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;public class DBUtil { static SQLiteDatabase sld; public static void createOrOpenDatabase() throws Exception { //目录是 /data/data/包名/数据库名 sld = SQLiteDatabase.openDatabase("/data/data/com.example.dbtest/dbtest", null, SQLiteDatabase.OPEN_READWRITE|SQLiteDatabase.CREATE_IF_NECESSARY); String sql = "create table if not exists user(username varchar2(20),password varchar2(20))"; sld.execSQL(sql); } public static void closeDatabase() throws Exception { try{ sld.close(); } catch(Exception e ){ e.printStackTrace(); } } public static List<String> searchuser(String username){ List<String> list = new ArrayList<String>(); try{ createOrOpenDatabase(); String sql = "select * from user where username='" +username + "'"; Cursor cur = sld.rawQuery(sql, new String[]{}); while(cur.moveToNext()){ list.add(cur.getString(0)); list.add(cur.getString(1)); } cur.close(); closeDatabase(); } catch (Exception e ){ e.printStackTrace(); } return list; } public static List<String> serchalluser(){ List<String> list = new ArrayList<String>(); try{ createOrOpenDatabase(); String sql = "select * from user"; Cursor cur = sld.rawQuery(sql, new String[]{}); while(cur.moveToNext()){ list.add(cur.getString(0)); list.add(cur.getString(1)); } cur.close(); closeDatabase(); } catch(Exception e ){ e.printStackTrace(); } return list; } public static void updatetable(String sql){ try{ createOrOpenDatabase(); sld.execSQL(sql); closeDatabase(); } catch(Exception e){ e.printStackTrace(); } } public static void droptable(){ try{ String sql ="drop table user"; createOrOpenDatabase(); sld.execSQL(sql); closeDatabase(); } catch(Exception e){ e.printStackTrace(); } }}
MainActivity的代码如下:
package com.example.dbtest;import java.util.List;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity { Button btn_createdb,btn_insert,btn_show,btn_update,btn_showall,btn_deleteusertable; EditText username,password; TextView show; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } public void init(){ username = (EditText) findViewById(R.id.username); password = (EditText) findViewById(R.id.password); btn_createdb = (Button) findViewById(R.id.btn_create); btn_insert = (Button) findViewById(R.id.btn_insert); btn_show = (Button) findViewById(R.id.btn_show); btn_update = (Button) findViewById(R.id.btn_update); btn_showall = (Button) findViewById(R.id.btn_showall); btn_deleteusertable = (Button) findViewById(R.id.btn_delet); show = (TextView) findViewById(R.id.showsomething); btn_createdb.setOnClickListener(listener); btn_insert.setOnClickListener(listener); btn_show.setOnClickListener(listener); btn_update.setOnClickListener(listener); btn_showall.setOnClickListener(listener); btn_deleteusertable.setOnClickListener(listener); } public OnClickListener listener = new OnClickListener(){ public void onClick(View v){ Button button = (Button)v; if(button.getId() == btn_createdb.getId()){ try{ DBUtil.createOrOpenDatabase(); show.setText("用户表已经打开"); } catch(Exception e){ e.printStackTrace(); } } else if(button.getId() == btn_update.getId()){ try{ String sql = "update user set username='" + username.getText() +"',password='" + password.getText() +"'"; DBUtil.updatetable(sql); show.setText("update success"); } catch(Exception e ){ e.printStackTrace(); } } else if(button.getId() == btn_insert.getId()){ try{ String sql = "insert into user values('" +username.getText() + "','" +password.getText()+"')"; DBUtil.updatetable(sql); show.setText("insert success"); } catch(Exception e ){ e.printStackTrace(); } } else if(button.getId() == btn_showall.getId()){ try{ List<String> list = DBUtil.serchalluser(); show.setText(list.toString()); } catch(Exception e ){ e.printStackTrace(); } } else if(button.getId() == btn_show.getId()){ try{ List<String> user =DBUtil.searchuser(username.getText().toString()); show.setText(user.toString()); } catch (Exception e ){ e.printStackTrace(); } } else if(button.getId() == btn_deleteusertable.getId()){ try{ DBUtil.droptable(); Toast.makeText(getApplicationContext(), "用户表删除成功", Toast.LENGTH_SHORT).show(); } catch (Exception e){ e.printStackTrace(); } } } }; @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; }}
运行结果如图:
0 0
- Android之sqlite操作
- Android之操作SQLite
- android 数据操作之SQLite
- Android之SQLite数据库操作
- Android之sqlite数据库操作
- Android SQLite操作之“SQLiteOpenHelper”
- Android开发之SQLite数据操作
- Android 学习之SQLite基本操作
- android之SQLite数据库insert操作
- Android数据读取之Sqlite数据库操作
- Android之SQLite操作与升级
- Android数据存储之操作SQLite
- android笔记之SQLite 数据库操作
- Android15--Android之SQLite数据库的操作
- Android 之SQLite数据库基本操作
- Android SQLite高级操作之泛型对象操作
- ios之SQLite操作
- SQLite--之操作
- python_学习笔记_基础练习_2
- 【Fragment精深系列7】Fragment切换优化
- bat 切换我的IP
- 完整java开发中JDBC连接数据库代码和步骤
- 在android里做一个竖着的seekbar
- Android之sqlite操作
- Android中View绘制流程以及invalidate()等相关方法分析
- 堆栈顺序存储结构实现3——遍历查看个数元素
- C++运算符重载(12) - 重载数组索引操作符[]
- android 实现手机方向识别
- nagios插件之监控多个tomcat线程数
- LeetCode 215:Kth Largest Element in an Array
- OpenWRT 路由配置技巧
- Android学习之 Scroller的介绍与使用