设置的标志在Android的SQLite数据库 [英] set flag in sqlite database in android

查看:126
本文介绍了设置的标志在Android的SQLite数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我想设置标志的收藏夹quote.On按钮,点击我要设置该标志为特殊的row.In我的数据库,对于报价我已经采取TEXT数据格式和嘘声精益BOOL.I已成功检索数据库中的文本价值,但我不能在database.As设置布尔值,我是新来这个主题有这方面的帮助,实在是AP preciated。 下面是我的code

 公共类DatabaseHelper扩展SQLiteOpenHelper {
    //而Android的默认系统应用程序的数据库路径。
    私有静态字符串DB_PATH =/data/data/com.t4t.smspopup/databases/;
    私有静态字符串DB_NAME =smspopupplus.sqlite;
    私有静态SQLiteDatabase MyDatabase的;
    私人最终语境myContext;
    公共静态字符串Column_Favorite =0;

    公共DatabaseHelper(上下文的背景下){
        超级(上下文,DB_NAME,空,1);
        this.myContext =背景;
    }

    公共无效的CreateDatabase()抛出IOException异常
    {
        布尔dbExist = checkDataBase();
        SQLiteDatabase db_Read = NULL;
        如果(dbExist)
        {

        }
        其他
        {
            db_Read = this.getReadableDatabase();
            db_Read.close();
            Log.i(调试++++,DATABSE创建);
            尝试
            {
                copyDataBase();
            }
            赶上(IOException异常E)
            {
                e.printStackTrace();
                抛出新的错误(错误复制数据库);
            }
        }
    }

    公共布尔checkDataBase()
    {
        SQLiteDatabase CHECKDB = NULL;

        尝试
        {
            字符串mypath中= DB_PATH + DB_NAME;
            CHECKDB = SQLiteDatabase.openDatabase(mypath中,空,SQLiteDatabase.OPEN_READONLY);
        }
        赶上(SQLiteException E)
        {

        }

        如果(CHECKDB!= NULL)
        {
            checkDB.close();
        }

        返回CHECKDB!= NULL?真假;
    }

    公共无效copyDataBase()抛出IOException异常
    {
        InputStream的myInput = myContext.getAssets()开(DB_NAME)。
        字符串outFileName = DB_PATH + DB_NAME;
        的OutputStream myOutput =新的FileOutputStream(outFileName);
        byte []的缓冲区=新的字节[1024];
        INT长;
        而((长度= myInput.read(缓冲液))大于0)
        {
            myOutput.write(缓冲液,0,长度);
        }

        myOutput.flush();
        myOutput.close();
        myInput.close();
    }

    公共无效的openDatabase()抛出的SQLException
    {
        字符串mypath中= DB_PATH + DB_NAME;
        MyDatabase的= SQLiteDatabase.openDatabase(mypath中,空,SQLiteDatabase.OPEN_READWRITE);
        Log.i(调试++++,DATABSE开);
    }

    公共无效的close()
    {
        如果(MyDatabase的!= NULL)
            myDataBase.close();

        super.close();
    }

    @覆盖
    公共无效的onCreate(SQLiteDatabase DB)
    {

    }

    @覆盖
    公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页)
    {

    }

    公共布尔数据库更新(字符串状态,弦乐什么,int值)
    {
        返回false;

    }
    // retrive从DB短信
    公众的ArrayList<字符串> getSMSList(字符串categoryName)
    {
        ArrayList的<字符串> SMSList =新的ArrayList<字符串>();

        尝试{
            光标光标= NULL;


            光标= myDataBase.query(真,类别名称,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
            而(cursor.moveToNext())
            {
                // ProgressData进展=新ProgressData(cursor.getString(0),cursor.getString(1));
                串SMSString = cursor.getString(1);
                SMSList.add(SMSString);
                //Log.i("debug++++,+ progressList);
            }
            cursor.close();

        }赶上(例外五){
            e.printStackTrace();
        }

        返回SMSList;
    }


    @燮pressWarnings(空)
    公共无效execSql(SQL字符串,对象[]参数)
    {
        SQLiteDatabase sqLiteDatabase = NULL;
        如果(参数== NULL)
        {
            sqLiteDatabase.execSQL(SQL);
        }
        其他

        {
            Log.e(调试-----------,命令execuated);
            sqLiteDatabase.execSQL(SQL,参数);
        }

    }
 

解决方案

SQLite的布尔数据类型:
SQLite没有一个单独的布尔存储类。相反,布尔值作为整数0(假)和1(真)存储。

您可以将布尔以这种方式来诠释:

  INT标志=(boolValue ==真)? 1:0;
查询字符串=UPDATE+ TABLE_NAME +设置+ COLUMN_NAME +=+标志+,其中+状态;
myDataBase.exeSQL(查询);
 

您可以将INT回布尔如下:

从数据库

  //选择COLUMN_NAME'标志'的值。
 //这将是整数值,可以按如下该int值转换回布尔
布尔FLAG2 =(的intValue == 1)真?假的;
 

In my application i want to set flag for favorites quote.On button click i want to set that flag for that particular row.In my database,for quotes i have taken TEXT data format and for boo lean BOOL.I have managed to retrieve text value from database but I'm not able to set Boolean value in database.As I'm new to this topic any help on this is really appreciated. Below is my code

public class DatabaseHelper extends SQLiteOpenHelper{
    //The Android's default system path of your application database.
    private static String DB_PATH = "/data/data/com.t4t.smspopup/databases/";
    private static String DB_NAME = "smspopupplus.sqlite";
    private static SQLiteDatabase myDataBase; 
    private final Context myContext;
    public static String Column_Favorite="0"; 

    public DatabaseHelper(Context context) {
        super(context, DB_NAME, null, 1);
        this.myContext = context;
    }

    public void createDataBase() throws IOException
    {
        boolean dbExist = checkDataBase();      
        SQLiteDatabase db_Read = null;
        if(dbExist)
        {

        }
        else
        {
            db_Read=this.getReadableDatabase();
            db_Read.close();
            Log.i("debug++++", "databse created");
            try
            {
                copyDataBase();
            }
            catch (IOException e)
            {
                e.printStackTrace();
                throw new Error("Error copying database");
            }
        }
    }

    public boolean checkDataBase()
    {
        SQLiteDatabase checkDB = null;

        try
        {
            String myPath = DB_PATH + DB_NAME;
            checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
        }
        catch(SQLiteException e)
        {

        }

        if(checkDB != null)
        {
            checkDB.close();
        }

        return checkDB != null ? true : false;
    }

    public void copyDataBase() throws IOException
    {
        InputStream myInput = myContext.getAssets().open(DB_NAME);
        String outFileName = DB_PATH + DB_NAME;
        OutputStream myOutput = new FileOutputStream(outFileName);
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer))>0)
        {
            myOutput.write(buffer, 0, length);
        }

        myOutput.flush();
        myOutput.close();
        myInput.close();
    }

    public void openDataBase() throws SQLException
    {
        String myPath = DB_PATH + DB_NAME;
        myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
        Log.i("debug++++", "databse opened");
    }

    public void close()
    {
        if(myDataBase != null)
            myDataBase.close();

        super.close();
    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {

    }

    public boolean updateDB(String status,String what,int value)
    {
        return false;

    }
    //retrive sms text from DB
    public  ArrayList<String> getSMSList(String categoryName)
    {
        ArrayList<String> SMSList = new ArrayList<String>();

        try{
            Cursor cursor=null;


            cursor = myDataBase.query(true,categoryName,null,null,null,null,null,null,null);
            while(cursor.moveToNext())
            {
                //ProgressData progress = new ProgressData(cursor.getString(0),cursor.getString(1));
                String SMSString=cursor.getString(1);
                SMSList.add(SMSString);
                //Log.i("debug++++", ""+progressList);
            }
            cursor.close();

        }catch (Exception e) {
            e.printStackTrace();
        }

        return SMSList;
    }


    @SuppressWarnings("null")
    public void execSql(String sql,Object[] args)
    {
        SQLiteDatabase sqLiteDatabase = null;
        if(args==null)
        {
            sqLiteDatabase.execSQL(sql);
        }
        else

        {
            Log.e("debug -----------", "command execuated");
            sqLiteDatabase.execSQL(sql,args);
        }

    } 

解决方案

SQLite Boolean Datatype:
SQLite does not have a separate Boolean storage class. Instead, Boolean values are stored as integers 0 (false) and 1 (true).

You can convert boolean to int in this way:

int flag = (boolValue==true)? 1:0;
String query = "UPDATE "+ TABLE_NAME + " set "+COLUMN_NAME+" = "+flag + " where "+CONDITION;
myDataBase.exeSQL(query);

You can convert int back to boolean as follows:

 // Select COLUMN_NAME 'flag' values from db. 
 // This will be integer value, you can convert this int value back to Boolean as follows
Boolean flag2 = (intValue==1)?true:false;

这篇关于设置的标志在Android的SQLite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆