android sqlite 获取总行数,Android SQLite大行数

需要在SQLite数据库中本地存储大约20,000行信息??.只有两列包含短文本字符串,因此实际的数据量很小,但我很难在设备上花3-5分钟插入行.我试过直接循环遍历数据集并调用一个简单的WritableDatabase.Insert,但这需要很长时间.所以我做了一些研究并被带到了the InsertHelper class,其中作者网站以大约900行/秒的速度处理行(这应该让我在20-30秒).我仍然无法在3-5分钟内处理数据.我错过了什么?性能因设备而异吗?使用ZTE Optik Android 3.2.1.

public class SqLiteHelper : SQLiteOpenHelper

{

private const string DATABASE_NAME = "NAME";

private const int DATABASE_VERSION = 1;

private readonly Context _context;

public SqLiteHelper(Context context)

: base(context, DATABASE_NAME, null, DATABASE_VERSION)

{

_context = context;

}

public override void OnCreate(SQLiteDatabase db)

{

try

{

db.ExecSQL("Create Table Inventory (ItemNumber Text Primary Key Not Null, ItemDescription Text);");

}

catch (SQLiteException ex)

{

Toast.MakeText(_context, ex.Message, ToastLength.Long).Show();

}

}

public override void OnUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)

{

}

}

class InventoryRepository : AsyncTask

{

private SqLiteHelper Helper { get; set; }

private Context Context { get; set; }

public InventoryRepository(SqLiteHelper helper, Context context)

{

Helper = helper;

Context = context;

}

protected override Java.Lang.Object DoInBackground(params Java.Lang.Object[] @params)

{

WS ws = new WS();

DataTable parts = ws.GetInventory(); //Web service getting inventory items from Server

Helper.WritableDatabase.Delete("Inventory", null, null);

DatabaseUtils.InsertHelper ih = new DatabaseUtils.InsertHelper(Helper.WritableDatabase, "Inventory");

Helper.WritableDatabase.SetLockingEnabled(false);

int partColumn = ih.GetColumnIndex("ItemNumber");

int partDescColumn = ih.GetColumnIndex("ItemDescription");

Helper.WritableDatabase.BeginTransaction();

try

{

foreach (DataRow part in parts.Rows)

{

try

{

ih.PrepareForInsert();

ih.Bind(partColumn, part[0].ToString().Replace("'", "''"));

ih.Bind(partDescColumn, part[1].ToString().Replace("'", "''"));

ih.Execute();

}

catch (SQLiteException ex)

{

if (ex.Message.Contains("constraint"))

continue;

throw;

}

catch (NullReferenceException e)

{

continue;

}

}

Helper.WritableDatabase.SetTransactionSuccessful();

}

finally

{

Helper.WritableDatabase.EndTransaction();

Helper.WritableDatabase.SetLockingEnabled(true);

ih.Close();

}

return "Done";

}

protected override void OnPostExecute(Java.Lang.Object result)

{

PreferenceManager.GetDefaultSharedPreferences(Context).Edit().PutString("LastInventoryUpdate", DateTime.Today.ToShortDateString()).Commit();

Intent intent = new Intent(Context, typeof(Login));

intent.AddFlags(ActivityFlags.NewTask);

Context.StartActivity(intent);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值