Android的SQLite的:如何生成用于测试目的的大表? [英] Android SQLite: How to generate big table for testing purpose?

查看:179
本文介绍了Android的SQLite的:如何生成用于测试目的的大表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我测试的设备的能力。 - 显示顾客可存储装置,它可以如何快速检索内部的数据的大小,如何快速搜索作品等

I am testing the capabilities of the device -- to show the customer the size of data that can be stored inside the device, how fast it can be retrieved, how fast the search works, etc.

我用我的内容提供商访问产品数据库中的表与几列。我已经搬到了code到内容提供商,以避免额外的通信插入测试记录时。下面code是通过菜单中调用从活动来填充表测试内容

I am using my content provider to access the product database table with few columns. I have already moved the code to the content provider to avoid the extra communication when inserting the test records. The following code is called via menu from an activity to fill the table with the test content

Uri uri = Uri.parse(DemoContentProvider.PRODUCTS_CONTENT_URI + "/insertdemo");
getContentResolver().insert(uri, null);

该URI在内容提供商的 .insert 法确认和(同内容提供商)以下的私有方法被调用来填充表(注意100上千项):

The URI is recognized in the .insert method of the content provider and the following private method (of the same content provider) is called to fill the table (notice the 100 thousands of items):

private void insertDemoProducts() {
    for (int i = 1; i <= 100000; ++i) {
        String id = Integer.toString(i);
        insertProduct(id, "Test product " + id, "100", "75.50", "70.27");
    }
}

insertProduct()看起来像:

private void insertProduct(String code, String name, String stock,
                           String price,  String listprice) {
    SQLiteDatabase sqlDB = database.getWritableDatabase();
    ContentValues values = new ContentValues();

    values.put(ProductTable.COLUMN_CODE, code);
    values.put(ProductTable.COLUMN_NAME, name);
    values.put(ProductTable.COLUMN_STOCK, stock);
    values.put(ProductTable.COLUMN_PRICE, price);
    values.put(ProductTable.COLUMN_LISTPRICE, listprice);

    sqlDB.insert(ProductTable.TABLE_PRODUCT, null, values);
}

它的工作原理,但它需要的永远的。我怎样才能使它更快吗?什么是你知道填写表格最快的方法是什么?

It works, but it takes "forever". How can I make it faster? What is the fastest method you know to fill the table?

只是一些数字来考虑:1000个项目大约需要20秒创建

Just some numbers to consider: 1000 items takes about 20 seconds to be created.

推荐答案

您需要使用事务写入一个SQLite数据库时,否则将持续在每次插入数据,即把它保存到SD将采取永远 。

You need to use transactions when writing to a sqlite-database, otherwise it will persist the data for every insert i.e save it to sd which will take "forever".

有关实例,请insertProduct采取的产品列表,并将其保存在一个事务:

for instance, make insertProduct take a list of products and save them in one transaction:

private void insertProducts(List<Product> products) {
    try {
        db.beginTransaction();
        for(Product product : products) {
            insertProduct(...);
        }
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
    }
}

这是如何在现有的code实现它:

This is how you can implement it in your existing code:

private void insertDemoProducts() {
    SQLiteDatabase sqlDB = database.getWritableDatabase();
    try {
        sqlDB.beginTransaction();
        for (int i = 1; i <= 100000; ++i) {
            String id = Integer.toString(i);
            insertProduct(id, "Test product " + id, "100", "75.50", "70.27");
        }
        sqlDB.setTransactionSuccessful();
    } finally {
        sqlDB.endTransaction();
    }
}

这篇关于Android的SQLite的:如何生成用于测试目的的大表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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