Android开发之SQLite的使用方法


当前第2页 返回上一页

        public void onClick(View v) {
            // TODO Auto-generated method stub
            DatabaseHelper database_helper = new DatabaseHelper(MainActivity.this, "tornadomeet.db");
            SQLiteDatabase db = database_helper.getWritableDatabase();
            //查询的语法,参数1为表名;参数2为表中的列名;参数3为要查询的列名;参数时为对应列的值;该函数返回的是一个游标
            Cursor cursor = db.query("user1", new String[]{"id", "name"}, "id=?", new String[]{"1"},  null, null, null);
            //遍历每一个记录
            while(cursor.moveToNext()) {
                String name = cursor.getString(cursor.getColumnIndex("name"));//返回列名为name的值
                System.out.println("query---->" + name);
            }
        }      
    }

    public class DeleteOnClickListener implements OnClickListener{

        public void onClick(View v) {
            // TODO Auto-generated method stub
            DatabaseHelper database_helper = new DatabaseHelper(MainActivity.this, "tornadomeet.db");
            SQLiteDatabase db = database_helper.getWritableDatabase();
            //直接删除名为tornadomeet对应的那条记录
            db.delete("user1", "name=?" ,new String[]{"tornadomeet"});
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

DatabaseHelper.java:

代码如下:

package com.example.sqlite_test;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {

    private static final int  VERSON = 1;//默认的数据库版本

    //继承SQLiteOpenHelper类的类必须有自己的构造函数
    //该构造函数4个参数,直接调用父类的构造函数。其中第一个参数为该类本身;第二个参数为数据库的名字;
    //第3个参数是用来设置游标对象的,这里一般设置为null;参数四是数据库的版本号。
    public DatabaseHelper(Context context, String name, CursorFactory factory, int verson){
        super(context, name, factory, verson);
    }

    //该构造函数有3个参数,因为它把上面函数的第3个参数固定为null了
    public DatabaseHelper(Context context, String name, int verson){
        this(context, name, null, verson);
    }

    //该构造函数只有2个参数,在上面函数 的基础山将版本号固定了
    public DatabaseHelper(Context context, String name){
        this(context, name, VERSON);
    }

    //该函数在数据库第一次被建立时调用
    @Override
    public void onCreate(SQLiteDatabase arg0) {
        // TODO Auto-generated method stub
        System.out.println("create a sqlite database");
        //execSQL()为执行参数里面的SQL语句,因此参数中的语句需要符合SQL语法,这里是创建一个表
        arg0.execSQL("create table user1(id int, name varchar(20))");
    }

    @Override
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
        // TODO Auto-generated method stub
        System.out.println("update a sqlite database");
    }

}

activity_main.xml:

代码如下:

<RelativeLayout 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" >

    <Button
        android:id="@+id/create_database"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="@string/create_database"
        />

    <Button
        android:id="@+id/update_database"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/create_database"
        android:layout_alignParentLeft="true"
        android:text="@string/update_database" />

    <Button
        android:id="@+id/insert"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/update_database"
        android:layout_alignParentLeft="true"
        android:text="@string/insert" />

    <Button
        android:id="@+id/update"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/insert"
        android:layout_alignParentLeft="true"
        android:text="@string/update" />

    <Button
        android:id="@+id/query"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_above="@id/update"
        android:text="@string/query"
        />
    <Button
        android:id="@+id/delete"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_above="@id/query"
        android:text="@string/delete"
        />

</RelativeLayout>

总结: 通过本次实验,对SQLite在andorid中的使用流程有了初步的了解。

附录:

  实验工程code下载


标签:SQLite

返回前面的内容

相关阅读 >>

scp远程vps快速搬家和wdcp升级php5.3安装memcached和eaccelerator教程

详解android数据存储之sqlcipher数据库加密

python 操作Sqlite数据库的示例

Sqlite中重置自动编号列的方法

利用apacheftpserver搭建ftp服务器的方法步骤

打造自己的.net core项目模板

android登录注册功能 数据库Sqlite验证

Sqlite3 命令行操作指南

python Sqlite的row对象操作示例

将txt文本内容导入Sqlite的方法

更多相关阅读请进入《Sqlite》频道 >>


数据库系统概念 第6版
书籍

数据库系统概念 第6版

机械工业出版社

本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...