在SQLite数据库中插入200多行的最佳方法

时间:2011-06-22 15:01:54

标签: android sqlite

我正在用数据库创建一个应用程序。我有一个表将有超过200行。 (应用程序中使用的参考表) 我创建了一个用于创建数据库并升级它的类。

我已经在类中创建了字符串来创建所需的表但是在类中添加了200多行它会变得非常大

是否有更好的方法来插入这些行。目前,它们位于应用程序中的SQL文件中

由于

3 个答案:

答案 0 :(得分:0)

您应该考虑使用事务并可能更改数据库的PRAGMA同步。 插入200多行将很长,使用事务将减少所需的时间:

sql.execSQL("BEGIN;");
//insert 1
//insert 2
// ...
sql.execSQL("END;");

将PRAGMA同步更改为0或1也会增加插入速度但可能会导致一些错误(插入时会删除wrting锁定)

有关详细信息,请参阅http://www.sqlite.org/pragma.htmlhttp://www.sqlite.org/lang_transaction.html

答案 1 :(得分:0)

您还可以在资源文件夹中附带预先填充的SQLite数据库,如本SO答案中所述:

Ship an application with a database

答案 2 :(得分:0)

您可以创建一个小的.net应用程序,以便在需要时填充表格。这是vb中的一个例子。它可以通过谷歌搜索到C#转换器轻松转换为c#。只需将代码复制并粘贴到转换器中即可。

    Dim myConnection As SqlConnection
    Dim myCommand As SqlCommand
    Dim i as Integer 

    'Connect to database
    myConnection = New SqlConnection("server=localhost;uid=sa;pwd=;database=yourdb") 
    myConnection.Open()

    'Insert rows, you may be able to use a for loop to make inserting easier
    myCommand = New SqlCommand("INSERT INTO yourtable VALUES( 12, _
    'IT Manager')",100,300,myConnection)
    i=myCommand.ExecuteNonQuery()

    'Close the connection

    myConnection.Close()