Android学习之SQLite

来源:互联网 发布:电棍在淘宝怎么搜 编辑:程序博客网 时间:2024/05/22 08:57
androidsqlite数据库buttonstringlayout

 

目录(?)[-]

  1. SQLite简介
  2. SQLite使用简介
  3. 在DOS环境对SQLite数据库的操作步骤

 

1.SQLite简介:

SQLite是一款轻型的数据库,它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常低,能够支持Windows/Linux/Unix等等主流的操作系统,同时可以喝很多种程序语言相结合,比如PHP、JAVA等。

SQLite虽然很小巧,但是支持的SQL语言不会逊色于其他开源数据库。

SQLite数据库的核心引擎不需要依赖第三方软件,也不需要所谓的“安装”。

SQLite数据库中所有的信息都包含在一个文件夹内,方便管理和维护。

SQLite数据库通过数据库级上的独占性和共享锁来实现独立的事务处理。这意味着多个线程可以再同一时间从同一数据库读取数据,但只能有一个可以写入数据。

2.SQLite使用简介:

本例子主要完成的功能是:点击一下按钮实现对数据库的建立、更新;以及对数据的插入、修改、查询、删除等功能;

首先自定义一个类继承SQLiteOpenHelper类;

<span style="background-color: rgb(255, 153, 255);">package com.douf.android.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 String DBNAME="test1";//定义要建立的数据库的版本号;private static final int VERSION=1;//构造器;分别为:上下文、表名、... 、版本号;public DatebaseHelper(Context context, String name, CursorFactory factory,int version) {super(context, DBNAME, null, version);}//创建数据库@Overridepublic void onCreate(SQLiteDatabase db) {db.execSQL("create table person(id integer primary key autoincrement,name varchar(20),password varchar(20))");}//更新数据库@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}}</span>


 

在main.xml中的配置定义几个简单的Button:

<span style="background-color: rgb(255, 153, 255);"><?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="wrap_content"    android:orientation="vertical" ><Button     android:id="@+id/createButton"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:text="@string/createdatabase"/><Button     android:id="@+id/updateButton"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:text="@string/updatedatabase"/><Button    android:id="@+id/insert"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:text="@string/insert"/><Button    android:id="@+id/update"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:text="@string/update"/><Button    android:id="@+id/query"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:text="@string/query"/><Button    android:id="@+id/delete"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:text="@string/delete"/></LinearLayout></span>


在string.xml中的一些配置:

<span style="background-color: rgb(255, 153, 255);"><?xml version="1.0" encoding="utf-8"?><resources>    <string name="hello">Hello World, SqliteActivity!</string>    <string name="app_name">Sqlite</string>    <string name="createdatabase">创建数据库</string>    <string name="updatedatabase">更新数据库</string>    <string name="insert">插入数据</string>    <string name="update">更新数据</string>    <string name="query">查询数据</string>    <string name="delete">删除数据</string></resources></span>


然后定义一个继承Activity类来完成对数据库的各种操作:

<span style="background-color: rgb(255, 153, 255);">package com.douf.android;import com.douf.android.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. *///定义main.xml中配置的Buttonprivate Button createButton;private Button updateButton;private Button insert;private Button update;private Button query;private Button delete;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                //通过findviewById来得到那些Button空间;        createButton=(Button) findViewById(R.id.createButton);        updateButton=(Button) findViewById(R.id.updateButton);        insert=(Button) findViewById(R.id.insert);        update=(Button) findViewById(R.id.update);        query=(Button) findViewById(R.id.query);        delete=(Button) findViewById(R.id.delete);                //为每个Button设置监听器        createButton.setOnClickListener(new ButtonListener1());        updateButton.setOnClickListener(new ButtonListener2());        insert.setOnClickListener(new ButtonListener3());        update.setOnClickListener(new ButtonListener4());        query.setOnClickListener(new ButtonListener5());        delete.setOnClickListener(new ButtonListener6());    }    //创建数据库    class ButtonListener1 implements OnClickListener{@Overridepublic void onClick(View v) {//创建一个DatebaseHelper对象DatebaseHelper dbHelper=new DatebaseHelper(SqliteActivity.this, "DBNAME", null, 1);//只有调用了SQLiteOpenHelper的getReadableDatabase或者getWritableDatabase才能真正的创建数据库SQLiteDatabase db=dbHelper.getReadableDatabase();}    }    //更新数据库    class ButtonListener2 implements OnClickListener{@Overridepublic void onClick(View v) {DatebaseHelper dbHelpler=new DatebaseHelper(SqliteActivity.this, "DBNAME", null, 3);SQLiteDatabase db=dbHelpler.getReadableDatabase();}    }    //插入数据    class ButtonListener3 implements OnClickListener{@Overridepublic void onClick(View v) {DatebaseHelper dbHelper=new DatebaseHelper(SqliteActivity.this, "DBNAME", null, 3);SQLiteDatabase db=dbHelper.getWritableDatabase();ContentValues values=new ContentValues();    values.put("name", "zhangsan");values.put("password", "123456");db.insert("person", null, values);}    }    //修改数据    class ButtonListener4 implements OnClickListener{@Overridepublic void onClick(View v) {DatebaseHelper dbHelper=new DatebaseHelper(SqliteActivity.this, "DBNAME", null, 3);SQLiteDatabase db=dbHelper.getWritableDatabase();ContentValues values=new ContentValues();values.put("name", "zhangsan");db.update("person", values, "id=?", new String[]{"1"});}    }    //查询数据    class ButtonListener5 implements OnClickListener{@Overridepublic void onClick(View v) {DatebaseHelper dbHelper=new DatebaseHelper(SqliteActivity.this, "DBNAME", null, 3);SQLiteDatabase db=dbHelper.getReadableDatabase();Cursor cursor=db.query("person", new String[]{"id","name","password"}, "id=?", new String[]{"1"}, null, null, null);while (cursor.moveToNext()) {int id=cursor.getInt(cursor.getColumnIndex("id"));String name=cursor.getString(cursor.getColumnIndex("name"));String password=cursor.getString(cursor.getColumnIndex("password"));System.out.println(id+"\t"+name+"\t"+password);}}    }    //删除数据    class ButtonListener6 implements OnClickListener{@Overridepublic void onClick(View v) {DatebaseHelper dbHelper=new DatebaseHelper(SqliteActivity.this, "DBNAME", null, 3);SQLiteDatabase db=dbHelper.getWritableDatabase();db.delete("person", "id=?", new String[]{"1"});}    }}</span>

 

3.在DOS环境对SQLite数据库的操作步骤:

adb shell

cd data

cd data

cd com.douf.android 进入本实例的包

cd databases 进入此实例SQLite数据库

sqlite3 建立的数据库名

.schema 可以查看此数据库的

这时就可以通过简单的SQL语句进行操作了。

以上就是对SQLite的简单使用。

 

原创粉丝点击