android中sqlite数据库的基本使用和添加多张表

Posted dogdogwang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android中sqlite数据库的基本使用和添加多张表相关的知识,希望对你有一定的参考价值。

看了很多关于android使用sqlite数据库的文章,很多都是介绍了数据库的建立和表的建立,而表通常都是只建立一张,而实际情况我们用到的表可能不止一张,那这种情况下我们又该怎么办呢,好了,下面我教大家如何在sqlite数据库中建立多张表。

首先是如何建立数据库和表:

建立一个类继承SQLiteOpenHelper,即:

public class ReaderOpenHelper extends SQLiteOpenHelper

然后添加构造方法:

public ReaderOpenHelper(Context context) 
        
        super(context, "people.db", null, 1);
        
    

people.db是数据库名字,1是数据库版本。

然后在该类实现以下两个方法:

技术图片
@Override
    public void onCreate(SQLiteDatabase db) 
        // TODO Auto-generated method stub
     db.execSQL("create table readers(renumber integer primary key,rename text,retype text,reage text,rephone text,usename integer,password integer,createtime text)");
     db.execSQL("create table books(booknumber integer primary key,bookname text,booktype text,bookeditor text,intime text,incounts integer)");
    
技术图片

db.execSQL的作用是执行SQL语句,create table readers是创建一个叫readers的表,括号里就是各个字段名和值类型。

这里创建了两张表。另一张叫books

继续实现:

技术图片
@Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
        // TODO Auto-generated method stub
        db.execSQL("drop if table exists readers");
        db.execSQL("drop if table exists  books");
        onCreate(db);
    
技术图片

执行Sql语句"drop if table exists 表名"

这样你的sqlite数据库和两张表就创建完成了。

接下来再建一个数据库manager类,如:

public class ReaderManager 

添加一个构造方法

public ReaderManager(Context conetxt) 
        this.context = context;
        readerOpenHelper = new ReaderOpenHelper(conetxt);
    

然后添加表的操作方法:

技术图片
package com.zhou.db;

import java.util.ArrayList;
import java.util.List;

import com.zhou.utils.Books;
import com.zhou.utils.Reader;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class ReaderManager 
    ReaderOpenHelper readerOpenHelper;
    Context context;

    public ReaderManager(Context conetxt) 
        this.context = context;
        readerOpenHelper = new ReaderOpenHelper(conetxt);
    

    // 增加读者
    public void addSQL(Reader reader) 
        SQLiteDatabase db = null;
        try 
            db = readerOpenHelper.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put("renumber", reader.getReNumber());
            values.put("rename", reader.getName());
            values.put("retype", reader.getSex());
            values.put("reage", reader.getAge());
            values.put("rephone", reader.getPhoneNumber());
            values.put("createtime", reader.getCreateTime());
            values.put("usename", reader.getUseName());
            values.put("password", reader.getPassword());
            db.insert("readers", null, values);
         catch (Exception e) 
            // TODO: handle exception
         finally 
            db.close();
        
    

    // 增加图书
    public void bookAddSQL(Books book) 
        SQLiteDatabase db = null;
        try 
            db = readerOpenHelper.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put("booknumber", book.getBookNumber());
            values.put("bookname", book.getBookName());
            values.put("booktype", book.getBookType());
            values.put("bookeditor", book.getBookEditer());
            values.put("intime", book.getInTime());
            values.put("incounts", book.getCount());
            db.insert("books", null, values);
         catch (Exception e) 
            // TODO: handle exception
         finally 
            db.close();
        
    

    // 读者查询
    public List<Reader> selectSQL() 
        List<Reader> list = new ArrayList<Reader>();
        SQLiteDatabase db = null;
        // 获取一个光标对象
        Cursor cursor = null;
        try 
            db = readerOpenHelper.getReadableDatabase();
            cursor = db.query("readers", null, null, null, null, null, null);
            Reader reader = null;
            while (cursor.moveToNext()) 
                reader = new Reader();
                reader.setReNumber(cursor.getInt(cursor
                        .getColumnIndex("renumber")));

                reader.setName(cursor.getString(cursor.getColumnIndex("rename")));
                reader.setSex(cursor.getString(cursor.getColumnIndex("retype")));
                reader.setAge(cursor.getString(cursor.getColumnIndex("reage")));
                reader.setPhoneNumber(cursor.getString(cursor
                        .getColumnIndex("rephone")));
                reader.setCreateTime(cursor.getString(cursor
                        .getColumnIndex("createtime")));
                reader.setUseName(cursor.getInt(cursor
                        .getColumnIndex("usename")));
                reader.setPassword(cursor.getInt(cursor
                        .getColumnIndex("password")));
                list.add(reader);
            
         catch (Exception e) 
            // TODO: handle exception
         finally 
            cursor.close();
            db.close();
        

        return list;
    

    // 图书查询
    public List<Books> bookSelectSQL() 
        List<Books> list = new ArrayList<Books>();
        SQLiteDatabase db = null;
        // 获取一个光标对象
        Cursor cursor = null;
        try 
            db = readerOpenHelper.getReadableDatabase();
            cursor = db.query("books", null, null, null, null, null, null);
            Books book = null;
            while (cursor.moveToNext()) 
                book = new Books();
                book.setBookNumber(cursor.getInt(cursor
                        .getColumnIndex("booknumber")));
                book.setBookName(cursor.getString(cursor
                        .getColumnIndex("bookname")));
                book.setBookType(cursor.getString(cursor
                        .getColumnIndex("booktype")));
                book.setBookEditer(cursor.getString(cursor
                        .getColumnIndex("bookeditor")));
                book.setInTime(cursor.getString(cursor.getColumnIndex("intime")));
                book.setCount(cursor.getInt(cursor.getColumnIndex("incounts")));
                Log.d("TAGG", cursor.getString(cursor.getColumnIndex("intime")));
                list.add(book);
            
         catch (Exception e) 
            // TODO: handle exception
         finally 
            cursor.close();
            db.close();
        

        return list;
    

    // 读者删除
    public void deleteSQL(final long id) 
        SQLiteDatabase db = null;
        try 
            db = readerOpenHelper.getWritableDatabase();
            db.delete("readers", "renumber=" + id, null);
         catch (Exception e) 

         finally 
            db.close();
        
    

    // 读者更新

    public void updateData(int id, String name, String sex, String age,
            String phoneNumber, int password) 

        SQLiteDatabase db = null;
        try 
            db = readerOpenHelper.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put("rename", name);
            values.put("retype", sex);
            values.put("reage", age);
            values.put("rephone", phoneNumber);
            values.put("password", password);
            db.update("readers", values, "renumber=" + id, null);
         catch (Exception e) 
            // TODO: handle exception
         finally 
            db.close();
        

    

    // public long getCount() 
    //
    // Cursor cursor = null;
    // long count = 0;
    // SQLiteDatabase db = null;
    // try 
    // db = readerOpenHelper.getWritableDatabase();
    // cursor = db.query("readers", null, null, null, null, null, null);
    //
    // if (null != cursor) 
    // count = cursor.getCount();
    // cursor.close();
    // 
    //  catch (Exception e) 
    //  finally 
    // db.close();
    // 
    // return count;
    // 

以上是关于android中sqlite数据库的基本使用和添加多张表的主要内容,如果未能解决你的问题,请参考以下文章

SQLite数据库及在Android开发中的基本使用

android SQLITE的基本使用总结

将多个项目添加到 SQLite 数据库 Android

Android学习总结(十八) ———— SQLite数据库使用

Android SQLite数据库基本操作

Android 自带的SQLite的原生数据库基本使用