android中sqlite數據庫的基本使用和添加多張表

看了不少關於android使用sqlite數據庫的文章,不少都是介紹了數據庫的創建和表的創建,而表一般都是隻創建一張,而實際狀況咱們用到的表可能不止一張,那這種狀況下咱們又該怎麼辦呢,好了,下面我教你們如何在sqlite數據庫中創建多張表。java

首先是如何創建數據庫和表:android

創建一個類繼承SQLiteOpenHelper,即:sql

public class ReaderOpenHelper extends SQLiteOpenHelper

而後添加構造方法:數據庫

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

people.db是數據庫名字,1是數據庫版本。ide

而後在該類實現如下兩個方法:this

複製代碼
@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的表,括號裏就是各個字段名和值類型。spa

這裏建立了兩張表。另外一張叫bookscode

繼續實現:sqlite

複製代碼
@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; // }  }
相關文章
相關標籤/搜索