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 = ?"


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.class

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的子类

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();
    }
}

转载于:https://www.cnblogs.com/jxgapyw/p/5838039.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值