Eclipse Indigo + Android 2.2 + SQLite 2.6.22
Android SQLite数据库简单示例
SqLiteActivity.java
package com.cuit.edu.cn; import com.cuit.edu.cn.db.DateBaseHelper; 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 SqLiteActivity extends Activity { /** Called when the activity is first created. */ private Button createButton = null; private Button updatebaseButton = null; private Button insertButton = null; private Button updateButton = null; private Button queryButton = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); createButton = (Button)findViewById(R.id.createdatebase); updatebaseButton = (Button)findViewById(R.id.updateDatebase); insertButton = (Button)findViewById(R.id.insterButton); updateButton = (Button)findViewById(R.id.updateButton); queryButton = (Button)findViewById(R.id.queryButton); createButton.setText(R.string.create); updatebaseButton.setText(R.string.updatebase); insertButton.setText(R.string.insert); updateButton.setText(R.string.update); queryButton.setText(R.string.query); createButton.setOnClickListener(new createButtonListener()); updatebaseButton.setOnClickListener(new updatebaseButtonListener()); insertButton.setOnClickListener(new insertButtonListener()); updateButton.setOnClickListener(new updateButtonListener()); queryButton.setOnClickListener(new queryButtonListener()); } class createButtonListener implements OnClickListener { @Override public void onClick(View v) { // TODO Auto-generated method stub DateBaseHelper dbHelper = new DateBaseHelper(SqLiteActivity.this, "user_Base"); SQLiteDatabase db = dbHelper.getReadableDatabase(); } } class updatebaseButtonListener implements OnClickListener { @Override public void onClick(View v) { // TODO Auto-generated method stub System.out.println("b================"); DateBaseHelper dbHelper = new DateBaseHelper(SqLiteActivity.this, "user_Base",90); SQLiteDatabase db = dbHelper.getReadableDatabase(); dbHelper.onUpgrade(db, 90, 91); } } class insertButtonListener implements OnClickListener { @Override public void onClick(View v) { // TODO Auto-generated method stub ContentValues values = new ContentValues(); values.put("id", 1); values.put("name", "rock"); DateBaseHelper dbHelper = new DateBaseHelper(SqLiteActivity.this,"user_Base"); SQLiteDatabase db = dbHelper.getWritableDatabase(); db.insert("user", null, values); } } class updateButtonListener implements OnClickListener { @Override public void onClick(View arg0) { // TODO Auto-generated method stub DateBaseHelper dbHelper = new DateBaseHelper(SqLiteActivity.this, "user_Base"); SQLiteDatabase db = dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("name", "cara"); db.update("user", values, "id=?", new String[]{"1"}); } } class queryButtonListener implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub DateBaseHelper dbHelper = new DateBaseHelper(SqLiteActivity.this, "user_Base"); SQLiteDatabase db = dbHelper.getReadableDatabase(); Cursor cursor = db.query("user", new String[]{"id","name"}, "id=?", new String[]{"1"}, null, null, null); while(cursor.moveToNext()) { String name = cursor.getString(cursor.getColumnIndex("name")); System.out.println("------"+name); } } } }
DateBaseHelper.java
package com.cuit.edu.cn.db; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; public class DateBaseHelper extends SQLiteOpenHelper { private static final int VERSION = 1; public DateBaseHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); // TODO Auto-generated constructor stub } public DateBaseHelper(Context context,String name) { this(context,name,VERSION); } public DateBaseHelper(Context context,String name,int version) { this(context,name,null,VERSION); } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub System.out.println("createdataBase"); db.execSQL("create table user(id int, name varchar(20))"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub System.out.println("updataBase"); } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/createdatebase" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/updateDatebase" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/insterButton" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/updateButton" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/queryButton" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, SqLiteActivity!</string> <string name="app_name">SqLite</string> <string name="query">query</string> <string name="updatebase">updatebase</string> <string name="update">update</string> <string name="create">create</string> <string name="insert">insert</string> </resources>
1. 使用sqlite3命令需要在命令行界面使用;
2. 可以使用adb shell命令进入命令行界面,前提是模拟器需要开启,且成功配置Android环境变量(H:\Program files\android-sdk-windows\platform-tools);
3. 数据文件保存在此路径下:/data/data/com.cuit.edu.cn/databases
4. 使用数据库,使用sqlite3数据库名命令。
Android数据保存之SQLite方法
http://www.cnitblog.com/liaoqingshan/archive/2013/02/20/86986.html
Android sqlite3命令行操作方法
http://blog.csdn.net/ccwwff/article/details/5834482
Command Line Shell For SQLite
http://www.sqlite.org/sqlite.html
SQlite命令行工具的使用
http://www.cnblogs.com/hnrainll/archive/2011/04/22/2024627.html
SQLite命令行程序说明
http://blog.pfan.cn/lounger/26540.html
@Wentasy 博文仅供参考,欢迎大家来访。如有错误之处,希望批评指正。原创博文如需转载请注明出处,谢谢 :) [CSDN博客] |