Android SQLite数据库运行异步任务,如何使用

Android SQLite数据库运行异步任务,如何使用,android,sqlite,Android,Sqlite,我对SQLite有一个基本的困惑。我是否在main中创建数据库实例?SQLite是在后台线程上运行还是必须将其放入异步任务中?(如果是,它是否应该与检索信息的任务相同?) 我正在分析MainActivity中AsyncTask中的HTTP POST(我得到了需要监视其信息的车辆列表),我想将此ArrayList放入SQLite数据库 我如何实现这一点?现在,我有一个内部类扩展MainActivity中的AsyncTask,还有一个单独的java文件DatabaseHandler扩展了SQLite

我对SQLite有一个基本的困惑。我是否在main中创建数据库实例?SQLite是在后台线程上运行还是必须将其放入异步任务中?(如果是,它是否应该与检索信息的任务相同?)

我正在分析MainActivity中AsyncTask中的HTTP POST(我得到了需要监视其信息的车辆列表),我想将此ArrayList放入SQLite数据库

我如何实现这一点?现在,我有一个内部类扩展MainActivity中的AsyncTask,还有一个单独的java文件DatabaseHandler扩展了SQLiteOpenHelper

public class DatabaseHandler extends SQLiteOpenHelper {

    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "vehicleManager";

    // Vehicle table name
    private static final String TABLE_VEHICLE = "vehicle";

............. code
}

最好是从反向线程加载/保存到数据库。
对sqlite的调用是同步的,这意味着它们可以阻止您的UI线程


你可以使用相同的异步任务或其他任务,这与你的应用程序的设计和你想做什么有关。重要的是不要从主线程运行查询。

最好从反向线程加载/保存到数据库。
对sqlite的调用是同步的,这意味着它们可以阻止您的UI线程


你可以使用相同的异步任务或其他任务,这与你的应用程序的设计和你想做什么有关。重要的是不要从主线程运行查询。

正如@Plato所提到的,您可以从
AsyncTask
保存/读取数据,并且在您的评论中,您询问了如何在数据库
类中创建公共函数,因此:

public class DatabaseHandler extends SQLiteOpenHelper {

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "vehicleManager";

// Vehicle table name
private static final String TABLE_VEHICLE = "vehicle";

//public function to do stuff with the database
public static void saveVehicle(/*arguments*/){ //note you should somhow pass data you want to save.... you can create as many arguments as you need
     //Sql save logic with those arguments
}
当您需要保存数据时,您应该调用

DatabaseHandler.saveVehicle(/*arguments*/);

请注意,当您在类中实现该方法时,您可能需要一个返回值来判断数据是否真的保存了。

正如@Plato所提到的,您可以从
AsyncTask
保存/读取数据,并且在您的注释中,您询问了如何在数据库
类中创建公共函数,因此:

public class DatabaseHandler extends SQLiteOpenHelper {

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "vehicleManager";

// Vehicle table name
private static final String TABLE_VEHICLE = "vehicle";

//public function to do stuff with the database
public static void saveVehicle(/*arguments*/){ //note you should somhow pass data you want to save.... you can create as many arguments as you need
     //Sql save logic with those arguments
}
当您需要保存数据时,您应该调用

DatabaseHandler.saveVehicle(/*arguments*/);

请注意,当您在类中实现该方法时,您可能需要一个返回值来判断数据是否真的被保存了。

下面是我要做的,我创建了一个名为
数据库的类,它是
静态的
,并让所有查询和所有数据库访问都通过它。如果我需要什么,我会在
数据库
类中创建一个
静态
函数,该函数执行我需要的任何特定查询,并返回一个
游标
,然后在代码中处理它。这样,如果我需要更改任何查询,我只需要更改一个位置,而不需要运行代码来查找所有实例

当我调用
Database.open(context)
时,
Database
类在其内部创建
SQLiteHelper
类的实例。当然,对
数据库
的所有调用都来自
异步任务
或第二个线程

再说一次,这是我的个人设计,你可以随意喜欢,也可以自己设计

public final class Database {

    private static SQLHelper sqlhelper = null;
    private static SQLiteDatabase database = null;
    private static Context context = null;

    /** Prevents Instances */
    private Database(){};

    /**
     * Initiates the Database for access
     * @param context Application context
     */
    public static void initiate(Context context){
        if (sqlhelper == null)
            sqlhelper = new SQLHelper(context);

        if (Database.context == null)
            Database.context = context;
    }

    /**
     * Opens the database for reading
     * @throws SQLException if the database cannot be opened for reading
     */
    public static void openReadable() throws SQLException{
        if (database == null)
            database = sqlhelper.getReadableDatabase();
    }

    /**
     * Opens the database for writing
     * Defaults to Foreign Keys Constraint ON
     * @throws SQLException if the database cannot be opened for writing
     */
    public static void openWritable() throws SQLException{
        if ((database == null)? true : database.isReadOnly()) {
            openWritable(true);
        }
    }

    /**
     * Opens the database for writing
     * @param foreignKeys State of Foreign Keys Constraint, true = ON, false = OFF
     * @throws SQLException if the database cannot be opened for writing
     */
    public static void openWritable(boolean foreignKeys) throws SQLException{
        database = sqlhelper.getWritableDatabase();
        if (foreignKeys) {
            database.execSQL("PRAGMA foreign_keys = ON;");
        } else {
            database.execSQL("PRAGMA foreign_keys = OFF;");
        }
    }

    /**
     * Closes the database
     */
    public static void close(){
        if (database != null){
            database.close();
            database = null;
        }
        if (sqlhelper != null){
            sqlhelper.close();
            sqlhelper = null;
        }
    }

    /* Add functions here */
    public static Cursor selectNames(){
        openReadable();
        return database.rawQuery("SELECT * FROM names", null);
    }
}


对于活动的访问:

Database.initiate(getBaseContext());
Cursor c = Database.selectNames();
从片段:

Database.initiate(getActivity());
Cursor c = Database.selectNames();

下面是我要做的,我创建了一个名为
Database
的类,它是
static
,所有查询和所有数据库访问都要经过它。如果我需要什么,我会在
数据库
类中创建一个
静态
函数,该函数执行我需要的任何特定查询,并返回一个
游标
,然后在代码中处理它。这样,如果我需要更改任何查询,我只需要更改一个位置,而不需要运行代码来查找所有实例

当我调用
Database.open(context)
时,
Database
类在其内部创建
SQLiteHelper
类的实例。当然,对
数据库
的所有调用都来自
异步任务
或第二个线程

再说一次,这是我的个人设计,你可以随意喜欢,也可以自己设计

public final class Database {

    private static SQLHelper sqlhelper = null;
    private static SQLiteDatabase database = null;
    private static Context context = null;

    /** Prevents Instances */
    private Database(){};

    /**
     * Initiates the Database for access
     * @param context Application context
     */
    public static void initiate(Context context){
        if (sqlhelper == null)
            sqlhelper = new SQLHelper(context);

        if (Database.context == null)
            Database.context = context;
    }

    /**
     * Opens the database for reading
     * @throws SQLException if the database cannot be opened for reading
     */
    public static void openReadable() throws SQLException{
        if (database == null)
            database = sqlhelper.getReadableDatabase();
    }

    /**
     * Opens the database for writing
     * Defaults to Foreign Keys Constraint ON
     * @throws SQLException if the database cannot be opened for writing
     */
    public static void openWritable() throws SQLException{
        if ((database == null)? true : database.isReadOnly()) {
            openWritable(true);
        }
    }

    /**
     * Opens the database for writing
     * @param foreignKeys State of Foreign Keys Constraint, true = ON, false = OFF
     * @throws SQLException if the database cannot be opened for writing
     */
    public static void openWritable(boolean foreignKeys) throws SQLException{
        database = sqlhelper.getWritableDatabase();
        if (foreignKeys) {
            database.execSQL("PRAGMA foreign_keys = ON;");
        } else {
            database.execSQL("PRAGMA foreign_keys = OFF;");
        }
    }

    /**
     * Closes the database
     */
    public static void close(){
        if (database != null){
            database.close();
            database = null;
        }
        if (sqlhelper != null){
            sqlhelper.close();
            sqlhelper = null;
        }
    }

    /* Add functions here */
    public static Cursor selectNames(){
        openReadable();
        return database.rawQuery("SELECT * FROM names", null);
    }
}


对于活动的访问:

Database.initiate(getBaseContext());
Cursor c = Database.selectNames();
从片段:

Database.initiate(getActivity());
Cursor c = Database.selectNames();

因此,我将数据库代码放在一个单独的java文件中。我应该在main中创建一个数据库,然后在asynctask中更新?或者将异步任务放在我的数据库中code@benzabill你可以在那个类中创建公共函数,将数据保存到数据库中。你能举个例子吗,@J1and1so我的数据库代码保存在一个单独的java文件中。我应该在main中创建一个数据库,然后在asynctask中更新?或者将异步任务放在我的数据库中code@benzabill您可以在该类中创建公共函数,将数据保存到您的数据库中。您能举一个例子@J1and1alright,这样我就不需要在我的main方法中创建SQLite数据库,而数据库处理程序将拥有它吗?另外,异步任务中应该包含哪些部分?对databasehandler类的任何调用?您将在databasehandler中创建保存/更新等所有方法,并从代码的任何部分调用它们来读取/写入/更新数据。在异步任务中,您将调用类似databasehandler.saveVehicle(/*arguments*/)的函数;当然,您需要修改此代码以传递所有要保存的数据。好的,所以我不需要在我的主方法中创建SQLite数据库,而是数据库处理程序将拥有它?另外,异步任务中应该包含哪些部分?对databasehandler类的任何调用?您将在databasehandler中创建保存/更新等所有方法,并从代码的任何部分调用它们来读取/写入/更新数据。在异步任务中,您将调用类似databasehandler.saveVehicle(/*arguments*/)的函数;当然,你需要修改这段代码来传递你想要保存的所有数据。我对游标不太了解,但它比简单地返回ArrayList甚至HashMapNone有什么好处呢?根据代码需要,我有一些函数返回游标,另一些函数返回单个定制对象,还有一些返回对象数组,最后是对象列表。这就是美,让我