Android-SQLite

本文介绍了如何在Android应用中使用SQLite数据库,包括创建数据库、表及执行增删改查操作,通过示例代码展示了具体实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[返回目录]


SQLite 是一个轻量级数据库,大小才几十K,但是麻雀虽小,五脏俱全;

官方网址:http://www.sqlite.org

与Java的JDBC和ODBC不同,SQLite的增删改查是通过SQLiteOpenHelper和SQLiteDatabase进行的;


SQLiteOpenHelper使用方法

getReadableDatabase() 返回一个可读的SQLiteDatabase

getWriteableDatabase() 返回一个可写的SQLiteDatabase

onCreate(SQLiteDatabase db)

onOpen(SQLiteDatabase db)

onUpgrade(SQLiteDatabase db,int old)


与其他组件不同额事,SQLiteOpenHelper的onCreate方法并不是创建的时候会调用,而是当调用getReadableDatabase()或者getWriteableDatabase()时会被调用(只第一次的时候调用)


在使用SQLite时,需编写一个类继承SQLiteOpenHelper并实现其构造方法、onCreate、OnUpdate等方法;

如下:

DatabaseHelper.java

<span style="font-family:SimHei;font-size:18px;">package com.example.testsqlite;

import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHelper extends SQLiteOpenHelper {

	private  static int VERSION=1;	//数据库的版本

	public DatabaseHelper(Context context, String name, CursorFactory factory,
			int version, DatabaseErrorHandler errorHandler) {
		super(context, name, factory, version, errorHandler);
	}

	public DatabaseHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
	}
	
	public DatabaseHelper(Context context,String name,int version){
		this(context, name, null, version, null);
	}
	
	public DatabaseHelper(Context context,String name){
		super(context,name,null,VERSION,null);
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		String sql="create table user(id int,name varchar(20))";
		db.execSQL(sql);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
		//当构造方法传进的Version值不同时触发
		Log.e("Potato", "onUpgrade");
	}

}</span>

其中,2参数与3参数的构造方法是我自己写的,为了方便调用。

在OnCreate中(也就是第一个创建时,一般新建一张表,系统自动识别,有则不创建,无则创建);


MainActivity.java

<span style="font-family:SimHei;font-size:18px;">package com.example.testsqlite;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
	private Button buttonCreate;
	private Button buttonInsert;
	private Button buttonDelete;
	private Button buttonUpdate;
	private Button buttonQuery;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		buttonCreate=(Button)findViewById(R.id.create);
		buttonInsert=(Button)findViewById(R.id.insert);
		buttonDelete=(Button)findViewById(R.id.delete);
		buttonUpdate=(Button)findViewById(R.id.update);
		buttonQuery=(Button)findViewById(R.id.query);
		
		ButtonListener buttonListener=new ButtonListener();
		buttonCreate.setOnClickListener(buttonListener);
		buttonInsert.setOnClickListener(buttonListener);
		buttonDelete.setOnClickListener(buttonListener);
		buttonUpdate.setOnClickListener(buttonListener);
		buttonQuery.setOnClickListener(buttonListener);

	}
	
	class ButtonListener implements OnClickListener{	
		@Override
		public void onClick(View view) {
			if(view.getId()==R.id.create){
				DatabaseHelper databaseHelper=new DatabaseHelper(MainActivity.this, "db_potato"); //第二个参数是数据库的名字				
				SQLiteDatabase dbReader=databaseHelper.getReadableDatabase();
				Log.e("Potato", "创建成功!");
				dbReader.close();
			}
			if(view.getId()==R.id.insert){
				DatabaseHelper databaseHelper=new DatabaseHelper(MainActivity.this, "db_potato"); //第二个参数是数据库的名字
				SQLiteDatabase dbWriter=databaseHelper.getWritableDatabase();
				ContentValues values=new ContentValues();
				values.put("id", 1);
				values.put("name", "potato");
				dbWriter.insert("user", null, values);//表名,null,值对象
				Log.e("Potato", "插入成功!");
				dbWriter.close();		
			}
			if(view.getId()==R.id.delete){
				DatabaseHelper databaseHelper=new DatabaseHelper(MainActivity.this, "db_potato"); //第二个参数是数据库的名字
				SQLiteDatabase dbWriter=databaseHelper.getWritableDatabase();
				 dbWriter.delete("user", "id=?", new String[]{"1"});
				 Log.e("Potato", "删除成功!");
				 dbWriter.close();		
			}
			if(view.getId()==R.id.update){
				DatabaseHelper databaseHelper=new DatabaseHelper(MainActivity.this, "db_potato"); //第二个参数是数据库的名字
				SQLiteDatabase dbWriter=databaseHelper.getWritableDatabase();
				ContentValues newValue=new ContentValues();
				newValue.put("name", "tomato");
				dbWriter.update("user", newValue, "id=?", new String[]{"1"});
				 Log.e("Potato", "更改成功!");
			}
			if(view.getId()==R.id.query){
				DatabaseHelper databaseHelper=new DatabaseHelper(MainActivity.this, "db_potato"); //第二个参数是数据库的名字				
				SQLiteDatabase dbReader=databaseHelper.getReadableDatabase();
				Cursor cursor=dbReader.rawQuery("select * from user where id=?", new String[]{"1"});
				while(cursor.moveToNext()){
					Log.e("Potato", cursor.getString(cursor.getColumnIndex("name")));
				}
				dbReader.close();
			}	
		}
	}
}


</span>



activity_main.xml

<span style="font-family:SimHei;font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    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/create"
        android:layout_width="244dp"
        android:layout_height="wrap_content"
        android:text="create"/>

    <Button
        android:id="@+id/insert"
        android:layout_width="244dp"
        android:layout_height="wrap_content"
        android:text="insert" />

    <Button
        android:id="@+id/delete"
        android:layout_width="244dp"
        android:layout_height="wrap_content"
        android:text="delete" />

    <Button
        android:id="@+id/update"
       android:layout_width="244dp"
        android:layout_height="wrap_content"
        android:text="update" />

    <Button
        android:id="@+id/query"
        android:layout_width="244dp"
        android:layout_height="wrap_content"
        android:text="query" />

</LinearLayout>
</span>


下附adb操作SQLite

在adb中操作数据库

在dos界面中输入adb shell 进入Android模拟器的底层,即其Linux系统

 

如果进不去,有两个可能:

1.环境变量没配置好

2.没有打开虚拟机

3.未知原因,输入adbkill-server之后再输入adbshell即可解决

 

进入底层后,进行如下操作

cd data

cd data

cd 包名(如 cdcn.potato.Game)

cd databasees

sqlite3 数据库名

然后就可以对数据库进行

 

附:

.schema 可以查看表名

ls可以查看所有内容


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值