Android實用的SQLite資料庫工具類
每天一個小目標,早晚單車變摩托
一個實用的Sqlite的工具類,呼叫的時候只需DBUtils.getInstance().creads(this);即可完成建立資料庫
之後依次呼叫方法就好
DBUtils.java
package com.example.bookadmin.sqlite;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import com.example.bookadmin.bean.SongBean;
import com.example.bookadmin.tools.utils.LogUtils;
import java.util.ArrayList;
/**
* Created by Administrator on 2017/10/19.
* by:TaoHui
*/
public class DBUtils {
private static DBUtils dbUtils;
private SQLiteDatabase db;
/**
* 單例模式
* @return
*/
public static DBUtils getInstance(){
if(dbUtils == null){
dbUtils = new DBUtils();
return dbUtils;
}
return dbUtils;
}
/**
* 建立資料表
* @param contenxt 上下文物件
*/
public void creads(Context contenxt){
String path = contenxt.getCacheDir().getPath()+"/muisd.db";
db = SQLiteDatabase.openOrCreateDatabase(path,null);
String sql = "create table if not exists t_person" +
"(id integer primary key autoincrement," +
"bsid int(50) ,name text(50))";
db.execSQL(sql);//建立表
}
/**
* 查詢資料
* 返回List
*/
public ArrayList<SongBean> selectis() {
ArrayList<SongBean> list = new ArrayList<>();
Cursor cursor = db.query("t_person",null,null,null,null,null,null);
while (cursor.moveToNext()){
SongBean userBean = new SongBean();
int id = cursor.getInt(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
int bsid = cursor.getInt(cursor.getColumnIndex("bsid"));
userBean.setName(name);
userBean.setBsid(bsid);
userBean.setId(id);
list.add(userBean);
Log.e("--Main--", "==============selectis======"+id+"================"+name+"================"+bsid);
}
if(cursor != null){
cursor.close();
}
return list;
}
/**
* 根據ID刪除資料
* id 刪除id
*/
public int delData(int id){
int inde = db.delete("t_person","id = ?",new String[]{String.valueOf(id)});
Log.e("--Main--", "==============刪除了======================"+inde );
return inde;
}
/**
* 根據ID修改資料
* id 修改條碼的id
* bsid 修改的ID
* name 修改的資料庫
*/
public int modifyData(int id,int bsid, String name){
ContentValues contentValues = new ContentValues();
contentValues.put("name",name);
contentValues.put("bsid",id);
int index = db.update("t_person",contentValues,"id = ?",new String[]{String.valueOf(id)});
Log.e("--Main--", "==============修改了======================"+index );
return index;
}
/**
* 新增資料
* bsid 新增的資料ID
* name 新增資料名稱
*/
public long insertData(int bsid, String name){
ContentValues contentValues = new ContentValues();
contentValues.put("name",name);
contentValues.put("bsid",bsid);
long dataSize = db.insert("t_person",null,contentValues);
Log.e("--Main--", "==============insertData======================"+name+"================"+bsid);
return dataSize;
}
/**
* 查詢名字單個資料
* @param names
* @return
*/
public boolean selectisData(String names){
//查詢資料庫
Cursor cursor = db.query("t_person",null,"name = ?",new String[]{names},null,null,null);
while (cursor.moveToNext()){
return true;
}
return false;
}
}
相關文章
- SQLite Helper類,基於.net c#的SQLite資料庫操作類SQLiteC#資料庫
- android sqlite資料庫 新增資料AndroidSQLite資料庫
- SQLPro for SQLite Mac(SQLite資料庫管理工具)SQLiteMac資料庫
- C# SQLiteHelper類似SqlHelper類實現存取Sqlite資料庫C#SQLite資料庫
- 資料庫工具類實現資料庫
- android SQLite資料庫應用於草稿箱AndroidSQLite資料庫
- Android 中使用 SQLite 資料庫AndroidSQLite資料庫
- 【Android】資料儲存(三) 資料庫(SQLite)Android資料庫SQLite
- 用Julia 0.51操作sqlite資料庫SQLite資料庫
- Android 原生 SQLite 資料庫的一次封裝實踐AndroidSQLite資料庫封裝
- python用sqlite3模組操作sqlite資料庫PythonSQLite資料庫
- sqlite 資料庫的資料字典SQLite資料庫
- Android 封裝AsyncTask操作Sqlite資料庫Android封裝SQLite資料庫
- sqlite操作--- oracle資料庫中的資料導進sqliteSQLiteOracle資料庫
- C# SQLite資料庫 訪問封裝類C#SQLite資料庫封裝
- Android資料庫高手祕籍(1):SQLite命令Android資料庫SQLite
- Android資料庫高手祕籍(一):SQLite命令Android資料庫SQLite
- 在Android中檢視和管理sqlite資料庫AndroidSQLite資料庫
- Android資料庫Sqlite的基本用法及升級策略Android資料庫SQLite
- Android工具類庫Android
- 寫一個工具生成資料庫實體類資料庫
- 在 Android Studio 上除錯資料庫 ( SQLite )Android除錯資料庫SQLite
- 【Java】操作Sqlite資料庫JavaSQLite資料庫
- SQLite資料庫管理器:SQLPro for SQLite for MacSQLite資料庫Mac
- sqlite輕量型資料庫的介紹及應用SQLite資料庫
- 高效操控SQLite資料庫,盡在SQLPro for SQLite for MacSQLite資料庫Mac
- Android版本升級同時Sqlite資料庫的升級及之前資料的保留AndroidSQLite資料庫
- Python操作SQLite資料庫PythonSQLite資料庫
- Python 操作 SQLite 資料庫PythonSQLite資料庫
- sqlite3資料庫操作SQLite資料庫
- IOS資料儲存之Sqlite資料庫iOSSQLite資料庫
- 5個免費、跨平臺的SQLite資料庫視覺化工具SQLite資料庫視覺化
- 使用sqlite3 模組操作sqlite3資料庫SQLite資料庫
- Python資料庫模組(sqlite3,SQLite3)Python資料庫SQLite
- SQLite資料庫學習小結——Frameworks層實現SQLite資料庫Framework
- SQLPro for SQLite mac 2024.10啟用版 SQLite資料庫管理軟體SQLiteMac資料庫
- Sql Server資料庫資料匯入到SQLite資料庫中Server資料庫SQLite
- 如何在 SAP BTP Java 應用裡使用 SQLite 資料庫JavaSQLite資料庫