What are the best practices for SQLite on Android?

前端 未结 10 2015
自闭症患者
自闭症患者 2020-11-21 23:45

What would be considered the best practices when executing queries on an SQLite database within an Android app?

Is it safe to run inserts, deletes and select queries

10条回答
  •  情歌与酒
    2020-11-22 00:23

    after struggling with this for a couple of hours, I've found that you can only use one db helper object per db execution. For example,

    for(int x = 0; x < someMaxValue; x++)
    {
        db = new DBAdapter(this);
        try
        {
    
            db.addRow
            (
                    NamesStringArray[i].toString(), 
                    StartTimeStringArray[i].toString(),
                    EndTimeStringArray[i].toString()
            );
    
        }
        catch (Exception e)
        {
            Log.e("Add Error", e.toString());
            e.printStackTrace();
        }
        db.close();
    }
    

    as apposed to:

    db = new DBAdapter(this);
    for(int x = 0; x < someMaxValue; x++)
    {
    
        try
        {
            // ask the database manager to add a row given the two strings
            db.addRow
            (
                    NamesStringArray[i].toString(), 
                    StartTimeStringArray[i].toString(),
                    EndTimeStringArray[i].toString()
            );
    
        }
        catch (Exception e)
        {
            Log.e("Add Error", e.toString());
            e.printStackTrace();
        }
    
    }
    db.close();
    

    creating a new DBAdapter each time the loop iterates was the only way I could get my strings into a database through my helper class.

提交回复
热议问题