Androidが開発したSQliteデータベース
9771 ワード
まず、この文章を見た縁のある友达に一言を送ります.努力すれば、必ず収穫があります.
次に、本題を開始します.
SQliteデータベースは軽量級のデータベースで、組み込み機器に使用され、2019年6月に流行していた2大モバイル端末オペレーティングシステムで、Androidとiosがこのデータベースをサポートしています.
アンドロイド開発でsqliteデータベースを操作する方法について説明します.
1.データベースの作成と更新
Googleが提供するapiにはSQLiteOpenHelperというクラスがあります.私たちはこのクラスを継承して新しいクラスを書くだけです.新しいクラスでDbHelperメソッドを実装してデータベースファイルを作成します(sqliteのデータベースはモバイル端末装置のハードディスクにファイルとして格納されています).onCreateメソッドを実装してデータテーブル構造を作成し、その後の更新時にonUpgradeメソッドを実装することでデータテーブル構造を更新します.
次のコードの例を示します.
2.データベースの基本操作、削除・変更
データベースの添削・変更を実行するには、前述のクラスのオブジェクトをインスタンス化した後にクラスを呼び出すgetWritableDatabaseメソッドがSQLiteDatabaseタイプのデータベース・インスタンス・オブジェクトを返し、そのオブジェクトを取得した後、データベース・オブジェクトを呼び出すrawQueryメソッドがsql文を実行してクエリーを実行する必要があります.このメソッドパラメータは文字列タイプのsql文で、文では「?」を使用できます.変数プレースホルダとして、2番目のパラメータがObjectタイプの配列である疑問符が表す変数.このメソッドの戻り値は、カーソルクラスCursorのオブジェクトです.このオブジェクトは、クエリー結果セットを取得するインデックスです.(ポインタ)デフォルトでは、チェーンテーブルの先頭にある空の要素を指します.このカーソルクラスのmoveToNextメソッドを呼び出して、結果チェーンテーブルに結果があるかどうかを判断できます.また、moveToNextメソッドでインデックスを1つ下に移動することで、結果セットの最初の結果をカーソルが指します.結果のカーソルが得られた後、カーソルクラスのgetStringメソッドを呼び出すことができます.もちろん、その結果をgetすることもできますそのタイプには、対応する方法もあります.に表示されます.コンテンツが得られたら、クエリー操作を完了します.
挿入操作(インクリメント)、更新操作(改)、削除操作(削除)はこの手順と似ています.ただし、この3つの操作は結果を返す必要はなく、入力するだけでよいので、execSQL実行sql文というもう1つの方法が提供されています.この方法の最初のパラメータは依然としてsql文で、2番目のパラメータはObject配列タイプのパラメータです.これにより、sql文を使用して追加削除が実現されます.
正直に言うと、私は自分で理解して、書く時に理解することができて、しかし今理解したい人が理解できることを保証しません.そこで上のコードに続きます.次に、インスタンスコードを示します.
声明:転載して出典を声明して下さい、私の入門級の菜鳥、問題の大きい人はよけいに指導します:QQ 941131649
次に、本題を開始します.
SQliteデータベースは軽量級のデータベースで、組み込み機器に使用され、2019年6月に流行していた2大モバイル端末オペレーティングシステムで、Androidとiosがこのデータベースをサポートしています.
アンドロイド開発でsqliteデータベースを操作する方法について説明します.
1.データベースの作成と更新
Googleが提供するapiにはSQLiteOpenHelperというクラスがあります.私たちはこのクラスを継承して新しいクラスを書くだけです.新しいクラスでDbHelperメソッドを実装してデータベースファイルを作成します(sqliteのデータベースはモバイル端末装置のハードディスクにファイルとして格納されています).onCreateメソッドを実装してデータテーブル構造を作成し、その後の更新時にonUpgradeメソッドを実装することでデータテーブル構造を更新します.
次のコードの例を示します.
public class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context, "info.db", null, 1); // info
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table userinfo (_id integer primary key autoincrement,username varchar(20),password varchar(20))"); //sql id , username password
db.execSQL("create table movie (_id integer primary key autoincrement,"+
"movienumber varchar(20)," + //
"moviename varchar(20)," + //
"uptime varchar(20)," + //
"price varchar(20)," + //
"actor varchar(20)," + //
"ifsell varchar(20)," + //
"time varchar(20)," + //
"area varchar(20))"); //
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
2.データベースの基本操作、削除・変更
データベースの添削・変更を実行するには、前述のクラスのオブジェクトをインスタンス化した後にクラスを呼び出すgetWritableDatabaseメソッドがSQLiteDatabaseタイプのデータベース・インスタンス・オブジェクトを返し、そのオブジェクトを取得した後、データベース・オブジェクトを呼び出すrawQueryメソッドがsql文を実行してクエリーを実行する必要があります.このメソッドパラメータは文字列タイプのsql文で、文では「?」を使用できます.変数プレースホルダとして、2番目のパラメータがObjectタイプの配列である疑問符が表す変数.このメソッドの戻り値は、カーソルクラスCursorのオブジェクトです.このオブジェクトは、クエリー結果セットを取得するインデックスです.(ポインタ)デフォルトでは、チェーンテーブルの先頭にある空の要素を指します.このカーソルクラスのmoveToNextメソッドを呼び出して、結果チェーンテーブルに結果があるかどうかを判断できます.また、moveToNextメソッドでインデックスを1つ下に移動することで、結果セットの最初の結果をカーソルが指します.結果のカーソルが得られた後、カーソルクラスのgetStringメソッドを呼び出すことができます.もちろん、その結果をgetすることもできますそのタイプには、対応する方法もあります.に表示されます.コンテンツが得られたら、クエリー操作を完了します.
挿入操作(インクリメント)、更新操作(改)、削除操作(削除)はこの手順と似ています.ただし、この3つの操作は結果を返す必要はなく、入力するだけでよいので、execSQL実行sql文というもう1つの方法が提供されています.この方法の最初のパラメータは依然としてsql文で、2番目のパラメータはObject配列タイプのパラメータです.これにより、sql文を使用して追加削除が実現されます.
正直に言うと、私は自分で理解して、書く時に理解することができて、しかし今理解したい人が理解できることを保証しません.そこで上のコードに続きます.次に、インスタンスコードを示します.
public class DbTools {
private DbHelper helper;
private SQLiteDatabase userinfodb;
public DbTools(Context context){
helper=new DbHelper(context); //
userinfodb=helper.getWritableDatabase(); //
}
/**
* ,
* @param username
* @param password
* @return
*/
public boolean register(String username,String password)
{
Cursor cu=userinfodb.rawQuery("select password from userinfo where username=?",new String[]{username});
boolean result=cu.moveToNext();
if(result)
{
cu.close();
return false;
}else
{
cu.close();
userinfodb.execSQL(" INSERT INTO userinfo (username,password) VALUES (?,?)",new Object[]{username,password});
return true;
}
}
/**
*
* @param username
* @param password
* @return
*/
public boolean login(String username,String password)
{
Cursor cu=userinfodb.rawQuery("select password from userinfo where username=?",new String[]{username});
boolean result=cu.moveToNext();
if(result)
{
if(password.equals(cu.getString(0))) {
cu.close();
return true;
}
else
{
cu.close();
return false;
}
}else
{
cu.close();
return false;
}
}
/**
*
* @param movienumber
* @param moviename
* @param uptime
* @param price
* @param actor
* @param ifsell
* @param time
* @param area
* @return
*/
public boolean addMovie(String movienumber,String moviename,String uptime,String price,String actor,String ifsell,String time,String area)
{
userinfodb.execSQL(" insert into movie (movienumber,moviename,uptime,price,actor,ifsell,time,area) VALUES (?,?,?,?,?,?,?,?)",new String[]{movienumber,moviename,uptime,price,actor,ifsell,time,area});
return true;
}
/**
*
*/
public static class Little{
public String name;
public String uptime;
public String id;
}
/**
*
*/
public static class Dital{
public String movienumber;
public String moviename;
public String uptime;
public String price;
public String actor;
public String ifsell;
public String time;
public String area;
}
/**
*
* @return
*/
public ArrayList getA()
{
ArrayList almovie=new ArrayList();
Cursor cu=userinfodb.rawQuery("select moviename,uptime,_id from movie",null);
while(cu.moveToNext())
{
Little amo=new Little();
amo.name=cu.getString(0);
amo.uptime=cu.getString(1);
amo.id=cu.getString(2);
almovie.add(amo);
}
cu.close();
return almovie;
}
/**
*
* @return
*/
public ArrayList getB(String name)
{
ArrayList almovie=new ArrayList();
Cursor cu=userinfodb.rawQuery("select moviename,uptime,_id from movie where moviename=?",new String[]{name});
while(cu.moveToNext())
{
Little amo=new Little();
amo.name=cu.getString(0);
amo.uptime=cu.getString(1);
amo.id=cu.getString(2);
almovie.add(amo);
}
cu.close();
return almovie;
}
/**
* id
* @param id
* @return
*/
public Dital getDital(String id){
Dital di=new Dital();
Cursor cu=userinfodb.rawQuery("select movienumber,moviename,uptime,price,actor,ifsell,time,area from movie where _id=?",new String[]{id});
cu.moveToNext();
di.movienumber=cu.getString(0);
di.moviename=cu.getString(1);
di.uptime=cu.getString(2);
di.price=cu.getString(3);
di.actor=cu.getString(4);
di.ifsell=cu.getString(5);
di.time=cu.getString(6);
di.area=cu.getString(7);
cu.close();
return di;
}
/**
*
* @param di
* @param id
*/
public void reEdit(Dital di,String id){
userinfodb.execSQL("update movie set movienumber=? where _id=?",new String[]{di.movienumber,id});
userinfodb.execSQL("update movie set moviename=? where _id=?",new String[]{di.moviename,id});
userinfodb.execSQL("update movie set uptime=? where _id=?",new String[]{di.uptime,id});
userinfodb.execSQL("update movie set price=? where _id=?",new String[]{di.price,id});
userinfodb.execSQL("update movie set actor=? where _id=?",new String[]{di.actor,id});
userinfodb.execSQL("update movie set ifsell=? where _id=?",new String[]{di.ifsell,id});
userinfodb.execSQL("update movie set time=? where _id=?",new String[]{di.time,id});
userinfodb.execSQL("update movie set area=? where _id=?",new String[]{di.area,id});
}
/**
*
* @param id
*/
public void delete(String id){
userinfodb.execSQL("delete from movie where _id=?",new String[]{id});
}
/**
*
* @param username
* @param password
*/
public boolean deleteuser(String username,String password){
Cursor cu=userinfodb.rawQuery("select password from userinfo where username=?",new String[]{username});
boolean result=cu.moveToNext();
if(result)
{
if(password.equals(cu.getString(0))){
userinfodb.execSQL("delete from userinfo where username=?",new String[]{username});
}
cu.close();
return true;
}else
{
cu.close();
return false;
}
}
/**
*
* @param username
* @param password
* @param newpassword
* @return
*/
public boolean reuser(String username,String password,String newpassword){
Cursor cu=userinfodb.rawQuery("select password from userinfo where username=?",new String[]{username});
boolean result=cu.moveToNext();
if(result)
{
if(password.equals(cu.getString(0))){
userinfodb.execSQL("update userinfo set password=? where username=?",new String[]{newpassword,username});
}else
{
cu.close();
return false;
}
cu.close();
return true;
}else
{
cu.close();
return false;
}
}
/**
*
*/
public void close()
{
userinfodb.close();
helper.close();
}
}
声明:転載して出典を声明して下さい、私の入門級の菜鳥、問題の大きい人はよけいに指導します:QQ 941131649