android中操做SQLite注意事項

  1. 調用rawQuery()後,須要調用Cursor的movToFirst();
public Cursor query(String sql, String[] args){
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(sql, args);
        cursor.moveToFirst();  // 重中之重,千萬不能忘了
        db.close();
        return cursor;
    }

2.刪除記錄時使用 sql ="delete from student where id = ?"java

sql ="delete from student where id = ?";
server.delete(sql, new Object[]{113});

public void delete(String sql, Object[] args){
        SQLiteDatabase db = this.getReadableDatabase();
        db.execSQL(sql, args);
        this.sql = sql;
        db.close();
    }

MainActivity.classandroid

package com.example.db1;

import java.util.Random;

import android.R.string;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;

public class DbServer extends SQLiteOpenHelper {
    private final static int DatabaseVersion = 1;
    private final static String DatabaseName = "abcd.db";
    public static int cnt = 111;
    public static String sql = null;
    
    public DbServer(Context context) {
        super(context, DatabaseName, null, DatabaseVersion);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        MainActivity.cnt = 1;
        this.cnt = 101;
        // TODO Auto-generated method stub
        String sql = "CREATE TABLE student (id INT PRIMARY KEY, NAME VARCHAR(10))";
        db.execSQL(sql);
        db.close();
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
          
    }

    public Cursor query(String sql, String[] args){
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(sql, args);
        cursor.moveToFirst();  // 重中之重,千萬不能忘了
        db.close();
        return cursor;
    }
    
    public void insert(String sql, Object[] args){
        SQLiteDatabase db = this.getReadableDatabase();
        this.sql = sql;
        db.execSQL(sql,args);
        db.close();
    }
    
    public void update(String sql, Object[] args){
        SQLiteDatabase db = this.getReadableDatabase();
        this.sql = sql;
        db.execSQL(sql,args);
        db.close();
    }
    
    public void delete(String sql, Object[] args){
        SQLiteDatabase db = this.getReadableDatabase();
        db.execSQL(sql, args);
        this.sql = sql;
        db.close();
    }
}

SQLiteOpenHelper的子類sql

package com.example.db1;

import java.util.Random;

import android.R.string;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;

public class DbServer extends SQLiteOpenHelper {
    private final static int DatabaseVersion = 1;
    private final static String DatabaseName = "abcd.db";
    public static int cnt = 111;
    public static String sql = null;
    
    public DbServer(Context context) {
        super(context, DatabaseName, null, DatabaseVersion);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        MainActivity.cnt = 1;
        this.cnt = 101;
        // TODO Auto-generated method stub
        String sql = "CREATE TABLE student (id INT PRIMARY KEY, NAME VARCHAR(10))";
        db.execSQL(sql);
        db.close();
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
          
    }

    public Cursor query(String sql, String[] args){
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(sql, args);
        cursor.moveToFirst();  // 重中之重,千萬不能忘了
        db.close();
        return cursor;
    }
    
    public void insert(String sql, Object[] args){
        SQLiteDatabase db = this.getReadableDatabase();
        this.sql = sql;
        db.execSQL(sql,args);
        db.close();
    }
    
    public void update(String sql, Object[] args){
        SQLiteDatabase db = this.getReadableDatabase();
        this.sql = sql;
        db.execSQL(sql,args);
        db.close();
    }
    
    public void delete(String sql, Object[] args){
        SQLiteDatabase db = this.getReadableDatabase();
        db.execSQL(sql, args);
        this.sql = sql;
        db.close();
    }
}
相關文章
相關標籤/搜索