zoukankan      html  css  js  c++  java
  • 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();
    	}
    }
    
  • 相关阅读:
    SDN第三次作业
    SDN第二次上机作业
    SDN第二次作业
    第七次作业之总结篇
    第八次_计算器重构
    第六次作业之计算器图形界面化
    C++课程 second work _1025
    第五次作业--计算器项目之学习文件读取方式
    C++课程 first work
    第四次作业-计算功能的实现
  • 原文地址:https://www.cnblogs.com/jxgapyw/p/5838039.html
Copyright © 2011-2022 走看看