SQLite使用

发布时间:2017-1-17 14:50:08 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"SQLite使用",主要涉及到SQLite使用方面的内容,对于SQLite使用感兴趣的同学可以参考一下。

SQLiteHelper工具类,主要目的是用于获取SQLIteDatabase对象 package com.liuxd.sqlite; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; /** * @ClassName SQLiteHelper * @Description SQLite使用工具类 * @author liuxd * @date 2013-8-20 下午5:20:25 * */ public class SQLiteHelper extends SQLiteOpenHelper { private static final int VERSION = 1; public SQLiteHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); } public SQLiteHelper(Context context, String name, int version) { this(context, name, null, version); } public SQLiteHelper(Context context, String name) { this(context, name, VERSION); } // 该函数在第一次创建数据库时执行,实际上是在第一次得到SQLDatabase对象时执行 @Override public void onCreate(SQLiteDatabase db) { System.out.println("create a database"); db.execSQL("create table user (id int,name varchar(20))"); } // 升级数据库时调用该方法 @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { System.out.println("upgrade database"); } } MainActivity程序入口类 package com.liuxd.sqlite; import android.os.Bundle; import android.app.Activity; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { private Button createButton; private Button upgradeButton; private Button insertButton; private Button updateButton; private Button queryButton; private Button deleteButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); createButton = (Button) findViewById(R.id.createButton); createButton.setOnClickListener(new CreateListener()); upgradeButton = (Button) findViewById(R.id.upgradeButton); upgradeButton.setOnClickListener(new UpgradeListener()); insertButton = (Button) findViewById(R.id.insertButton); insertButton.setOnClickListener(new InsertListener()); updateButton = (Button) findViewById(R.id.updateButton); updateButton.setOnClickListener(new UpdateListener()); queryButton = (Button) findViewById(R.id.queryButton); queryButton.setOnClickListener(new QueryListener()); deleteButton = (Button) findViewById(R.id.deleteButton); deleteButton.setOnClickListener(new DeleteListener()); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } class CreateListener implements OnClickListener { @Override public void onClick(View v) { SQLiteHelper h = new SQLiteHelper(MainActivity.this, "myDataBase"); // 在//获取SQLiteDatabase对象的时候创建数据库 SQLiteDatabase db = h.getReadableDatabase(); } } class QueryListener implements OnClickListener { @Override public void onClick(View v) { // 获取SQLiteDatabase对象 SQLiteHelper h = new SQLiteHelper(MainActivity.this, "myDataBase"); SQLiteDatabase db = h.getReadableDatabase(); Cursor cr = db.query("user", // 表名 new String[] { "id", "name" }, // 需要查询的列名 "id=?",// where条件 new String[] { "1" }, // where条件值 null, // group by 分组信息 null, // having 分组条件 "id");// order by 排序 // 移动指针获取每列数据 while (cr.moveToNext()) { String id = cr.getString(cr.getColumnIndex("id")); String name = cr.getString(cr.getColumnIndex("name")); System.out.println("id:" + id + ";name:" + name); } } } class DeleteListener implements OnClickListener { @Override public void onClick(View v) { // 获取SQLiteDatabase对象 SQLiteHelper h = new SQLiteHelper(MainActivity.this, "myDataBase"); SQLiteDatabase db = h.getReadableDatabase(); db.delete("user",// 表名 "id=?",// where条件 new String[] { "1" });// 查询条件值 System.out.println("delete over"); } } class UpdateListener implements OnClickListener { @Override public void onClick(View v) { // 获取SQLiteDatabase对象 SQLiteHelper h = new SQLiteHelper(MainActivity.this, "myDataBase"); SQLiteDatabase db = h.getReadableDatabase(); ContentValues values = new ContentValues(); values.put("name", "liuxd2"); db.update("user",// 表名 values, // 需要更新的数据 "id=?", // where条件 new String[] { "1" });// where条件值 System.out.println("update over"); } } class InsertListener implements OnClickListener { @Override public void onClick(View v) { // 获取SQLiteDatabase对象 SQLiteHelper h = new SQLiteHelper(MainActivity.this, "myDataBase"); SQLiteDatabase db = h.getReadableDatabase(); ContentValues values = new ContentValues(); values.put("id", 1); values.put("name", "liuxd"); db.insert("user", null, values); System.out.println("insert over"); } } class UpgradeListener implements OnClickListener { @Override public void onClick(View v) { // 获取SQLiteDatabase对象 SQLiteHelper h = new SQLiteHelper(MainActivity.this, "myDataBase"); SQLiteDatabase db = h.getReadableDatabase(); h.onUpgrade(db, 1, 2); System.out.println("upgrade over"); } } } 布局文件,很简单的线性布局 <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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/createButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="createDataBase" /> <Button android:id="@+id/upgradeButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="upgradeDataBase" /> <Button android:id="@+id/insertButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="insertDataBase" /> <Button android:id="@+id/updateButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="updateDataBase" /> <Button android:id="@+id/queryButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="queryDataBase" /> <Button android:id="@+id/deleteButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="deleteDataBase" /> </LinearLayout>

上一篇:set names和其他一些设置字符编码的命令区别
下一篇:nyoj 115

相关文章

关键词: SQLite使用

相关评论