在android中使用SQLite数据库

来源:互联网 发布:win7 禁止卸载软件 编辑:程序博客网 时间:2024/05/16 03:55

SQLite数据库以其轻量、体积小等特点,使其在开发中运用的非常广泛,在前面的博客中我也介绍过在Cocos2d-x中使用SQLite数据库,这篇博客是介绍在Android中使用SQLite数据库,Android中直接集成了SQLite数据库,使用起来非常方便,不需要向Cocos2d-x中那样添加外部文件

我将使用SQLite数据库实现一个下图所示的效果,打开app后会弹出下图所示的界面



单击createDatabase按钮后,在logcat中可以看到打印了一条提示信息,表示数据库创建成功,当再次点击createDAtabase按钮后就不会再打印提示信息,因为数据库创建成功后不会再创建数据库



数据库创建成功后可以在app所在的包下找到一个database文件夹,databases文件夹下可以找到创建好的数据库文件



将people.db导出后,在使用SQLite Expert可以查看创建好的数据库中的内容



单击Insert按钮后可以看到,向数据库中插入了10个学生的信息



按下Delete按钮后会删除"刘得意"信息,可以看到数据库中已经没有刘得意的信息了


单击Update按钮后,将所有学生的学号都减少了1


点击Select按钮后,查找数据库中所有学生的姓名和与姓名对应的C++成绩,并在logcat中输出


实现方式:
1、使用Android Studio创建一个Android工程,修改activity_main.xml文件

<?xml version="1.0" encoding="utf-8"?><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: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="com.fyt.databasedemo.MainActivity"    android:orientation="vertical">    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="createDatabase"        android:textSize="30dp"        android:onClick="createDatabase"/>    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Insert"        android:textSize="30dp"        android:onClick="Insert"/>    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Delete"        android:textSize="30dp"        android:onClick="Delete"/>    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Update"        android:textSize="30dp"        android:onClick="Update"/>    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Select"        android:textSize="30dp"        android:onClick="Select"/></LinearLayout>

2、新建一个MyOpenHelper.java文件,在MyOpenHelper.java中添加下面的代码

package com.fyt.databasedemo;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;import android.util.Log;//创建一个抽象类SQLiteOpenHelper的实现类MyOpenHelperpublic class MyOpenHelper extends SQLiteOpenHelper {    /**     * MyOpenHelper构造方法     * @param context 上下文     * @param name 数据库文件的名字     * @param factory 游标工厂(结果集)     * @param version 数据库的版本号(用于升级)     */    public MyOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {        super(context, name, factory, version);    }    //创建数据库时,调用此方法    @Override    public void onCreate(SQLiteDatabase db) {        Log.d("MainActivity", "数据库创建成功");        //创建一个学生表        db.execSQL("create table student(_id integer primary key autoincrement, name char(10), age integer, no integer, cpp float, math float, english float)");    }    //数据库升级时调用此方法    @Override    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {        Log.d("MainActivity", "数据库升级成功");    }}

3、修改MainActivity.java中的代码

package com.fyt.databasedemo;import android.app.Activity;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.os.Bundle;import android.util.Log;import android.view.View;public class MainActivity extends Activity {    //用于创建帮助器对象    private MyOpenHelper oh;    //用于创建数据库对象    private SQLiteDatabase db;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }    //创建数据库    public void createDatabase(View view) {        //创建帮助器对象        oh = new MyOpenHelper(this, "people.db", null, 1);        //创建数据库对象        db = oh.getWritableDatabase();    }    //向数据库中添加数据    public void Insert(View view) {        //向学生表中添加10名学生        db.execSQL("insert into student(name, age, no, cpp, math, english) values(?, ?, ?, ?, ?, ?)", new Object[]{"刘得意", 19, 1001, 60, 98, 75});        db.execSQL("insert into student(name, age, no, cpp, math, english) values(?, ?, ?, ?, ?, ?)", new Object[]{"王锐", 20, 1002, 63, 90, 96});        db.execSQL("insert into student(name, age, no, cpp, math, english) values(?, ?, ?, ?, ?, ?)", new Object[]{"何煜中", 19, 1003, 90, 73, 82});        db.execSQL("insert into student(name, age, no, cpp, math, english) values(?, ?, ?, ?, ?, ?)", new Object[]{"王磊", 21, 1004, 87, 86, 92});        db.execSQL("insert into student(name, age, no, cpp, math, english) values(?, ?, ?, ?, ?, ?)", new Object[]{"冯松", 19, 1005, 89, 98, 83});        db.execSQL("insert into student(name, age, no, cpp, math, english) values(?, ?, ?, ?, ?, ?)", new Object[]{"裴培", 20, 1006, 75, 82, 91});        db.execSQL("insert into student(name, age, no, cpp, math, english) values(?, ?, ?, ?, ?, ?)", new Object[]{"马骁", 19, 1007, 62, 67, 90});        db.execSQL("insert into student(name, age, no, cpp, math, english) values(?, ?, ?, ?, ?, ?)", new Object[]{"马婧", 20, 1008, 98, 84, 87});        db.execSQL("insert into student(name, age, no, cpp, math, english) values(?, ?, ?, ?, ?, ?)", new Object[]{"周俊升", 19, 1009, 57, 68, 96});        db.execSQL("insert into student(name, age, no, cpp, math, english) values(?, ?, ?, ?, ?, ?)", new Object[]{"贺祺", 21, 1010, 61, 96, 72});    }    //删除数据库中的数据    public void Delete(View view) {        //删除姓名为"刘得意"的学生的信息        db.execSQL("delete from Student where name = ?", new Object[]{"刘得意"});    }    //修改数据库中的数据    public void Update(View view) {        //将数据库中所有人的学号减少1        db.execSQL("update student set no = no -1");    }    //查询数据库中的数据    public void Select(View view) {        //查询数据库中学生的姓名和以其对应的C++成绩,返回值为一个结果集        Cursor cursor = db.rawQuery("select name, cpp from student", null);        while (cursor.moveToNext()) {            //cursor.getColumnIndex("name")获得姓名所在的列            String name = cursor.getString(cursor.getColumnIndex("name"));            float cpp = cursor.getFloat(cursor.getColumnIndex("cpp"));            //输出学生的姓名和与姓名对应的C++成绩            Log.d("MainActivity", '[' + name + ", " + cpp + ']');        }    }}

1 0
原创粉丝点击