简体   繁体   English

将大列表插入SQL数据库的最佳方法是什么? [ANDROID]

[英]What is the best way to insert large lists into a SQL database? [ANDROID]

Here is what i am trying to do, i have created a SQL database to hold a giant table of drinks, The table looks like this: 这是我正在尝试做的事情,我创建了一个SQL数据库来保存一个庞大的饮料表,该表如下所示:

    **_ID    ALCOHOL   TYPE    BRAND    PRICE**

Ex. 例如 1 Liquor Vodka Smirnoff 14 1酒伏特加Smirnoff 14

Here is the code for my database adapter: 这是我的数据库适配器的代码:

    package net.learn2develop.Database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBAdapter {

public static final String KEY_ROWID = "_id";
public static final String KEY_ALCOHOL = "alcohol";
public static final String KEY_TYPE = "type";
public static final String KEY_BRAND = "brand";
public static final String KEY_PRICE = "price";
private static final String TAG = "DBAdapter";

private static final String DATABASE_NAME = "booze";
private static final String DATABASE_TABLE = "titles";
private static final int DATABASE_VERSION = 1;

private static final String DATABASE_CREATE =
    "create table titles (_id integer primary key autoincrement, "
    + "alcohol text not null, type text not null, " 
    + "brand text not null);" + "price integer not null";

private final Context context;

private DatabaseHelper DBHelper;
private SQLiteDatabase db;

public DBAdapter(Context ctx) 
{
    this.context = ctx;
    DBHelper = new DatabaseHelper(context);
}

private static class DatabaseHelper extends SQLiteOpenHelper 
{
    DatabaseHelper(Context context) 
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) 
    {
        db.execSQL(DATABASE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, 
                          int newVersion) 
    {
        Log.w(TAG, "Upgrading database from version " + oldVersion 
              + " to "
              + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS titles");
        onCreate(db);
    }
}

  //---opens the database---
    public DBAdapter open() throws SQLException 
{
    db = DBHelper.getWritableDatabase();
    return this;
}

//---closes the database---    
public void close() 
{
    DBHelper.close();
}

//---insert a title into the database---
public long insertTitle(String alcohol, String type, String brand, int price) 
{
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_ALCOHOL, alcohol);
    initialValues.put(KEY_TYPE, type);
    initialValues.put(KEY_BRAND, brand);
    initialValues.put(KEY_PRICE, price);
    return db.insert(DATABASE_TABLE, null, initialValues);
}

//---deletes a particular title---
public boolean deleteTitle(long rowId) 
{
    return db.delete(DATABASE_TABLE, KEY_ROWID + 
            "=" + rowId, null) > 0;
}

//---retrieves all the titles---
public Cursor getAllTitles() 
{
    return db.query(DATABASE_TABLE, new String[] {
            KEY_ROWID, 
            KEY_ALCOHOL,
            KEY_TYPE,
            KEY_BRAND,
            KEY_PRICE}, 
            null, 
            null, 
            null, 
            null, 
            null);
}

//---retrieves a particular title---
public Cursor getTitle(long rowId) throws SQLException 
{
    Cursor mCursor =
            db.query(true, DATABASE_TABLE, new String[] {
                    KEY_ROWID,
                    KEY_ALCOHOL, 
                    KEY_TYPE,
                    KEY_BRAND,
                    KEY_PRICE
                    }, 
                    KEY_ROWID + "=" + rowId, 
                    null,
                    null, 
                    null, 
                    null, 
                    null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

//---updates a title---
public boolean updateTitle(long rowId, String alcohol, 
String type, String brand, int price) 
{
    ContentValues args = new ContentValues();
    args.put(KEY_ALCOHOL, alcohol);
    args.put(KEY_TYPE, type);
    args.put(KEY_BRAND, brand);
    args.put(KEY_PRICE, price);
    return db.update(DATABASE_TABLE, args, 
                     KEY_ROWID + "=" + rowId, null) > 0;
}

}

Now I know I could add them individually in my main activity by calling: 现在,我知道可以通过调用以下命令将它们分别添加到我的主要活动中:

    //---add 2 titles---
    db.open();        
    long id;
    id = db.insertTitle(
            "Liquor",
            "Vodka",
            "Smirnoff",
                    "14");        
    id = db.insertTitle(
            "Liquor",
            "Rum",
            "Captain Morgan",
                    "20");
    db.close();

The problem is i have hundreds to add.... what is the best way to go about adding them all into the DB? 问题是我要添加数百个...。将它们全部添加到数据库中的最佳方法是什么? Thanks in advance for anyhelp you guys! 预先感谢你们的任何帮助! I am still learning, i have never used a database before so im lost rite now. 我仍在学习,我以前从未使用过数据库,所以现在我失去了礼仪。

I wouldn't recommend hard coding them in your source, since you will have double the data (and then some) and will suck RAM and storage from your users. 我不建议在您的源代码中对其进行硬编码,因为您将获得两倍的数据(然后是两倍),并且会从用户那里获取RAM和存储空间。

I assume that there is no good reason for you to statically load the list into your DB at runtime. 我认为没有充分的理由让您在运行时将列表静态加载到数据库中。 I also assume that you already have the list of drinks. 我还假设您已经有了饮料清单。

I would create a CSV file from the list and then use a desktop SQLite editor on my PC to load the list into my table(s), then include the database as a resource in my app. 我将从列表中创建一个CSV文件,然后在PC上使用桌面SQLite编辑器将该列表加载到我的表中,然后将该数据库作为资源包含在我的应用程序中。

Google will lead you to plenty of SQLite editors. Google会带您前往大量SQLite编辑器。

I do all of my DB design and initial loading/testing on my PC before moving it to Android. 在将其移至Android之前,我先在PC上进行了所有数据库设计和初始加载/测试。 It's a lot easier and faster. 它变得更加容易和快捷。

Good luck. 祝好运。

You could do something like this: 您可以执行以下操作:

SQLiteDatabase db = mOpenHelper.getWritableDatabase();

db.beginTransaction();
try {   
    for(ContentValues con:conValues){
    db.insert(TABLE_COLUMN,null,con);
}
    db.setTransactionSuccessful();
} 
    finally {
    db.endTransaction();
}

You can read more about begintransaction and end transaction SQLite Optimization in Android 您可以在Android中阅读有关begintransaction和结束事务SQLite优化的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将数据插入数据库-最好的方法是什么 - Insert data into Database - What is the best way to do it 插入数据库的最佳方法? - Best way to insert into database? 在Android中的大型字符串数组中进行全文搜索的最佳方法是什么? - what is the best way to full text search in a large Array of String in android? 比较两个非常大的列表的最佳方法 - The best way to compare two very large lists java中大型列表的最佳列表实现是什么 - What is the best List implementation for Large lists in java 比较两个非常大的列表(不适合内存)的最佳方法是什么? - What is the best way to compare two very large lists(that do not fit in memory)? 将此图像(具有正确的尺寸)插入我的Android项目的最佳方法是什么? - What is the best way to insert this image (with the correct dimension) into my Android project? 在 Java 中存储多个列表的最佳方式是什么? - What is the best way to store multiple lists in Java? 处理大型CSV文件的最佳方法是什么? - What is the best way to process large CSV files? 将条目插入ElasticSearch的最佳方法是什么? - What is the Best way to insert Entries into ElasticSearch?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM