【问题标题】:Inserting multiple rows at once in sqlite database在sqlite数据库中一次插入多行
【发布时间】:2013-07-26 20:47:53
【问题描述】:

我正在尝试在 sqlite 数据库中插入多行。我创建了一个 SqliteOpenHelper 类并创建了一个数据库和一个表。现在我需要做的是在数据库中插入多行。

假设我们有五行要一次插入,什么是最好和最有效的方法来完成它。

INSERT INTO `events` (`_id`, `event_name`, `start_date`, `end_date`, `event_city`, `event_state`, `event_pic`, `event_desc`, `wiki_url`, `event_cat`) VALUES
(1, 'Purna Kumbh Fair 2013', '2013-01-27', '2013-03-10', 'Allahabad', 'Uttar Pradesh', 'img/kumbh.jpg', 'Kumbh Fair is a mass Hindu pilgrimage of faith in which Hindus gather at a sacred river for a bath in the river. It is held every third year at one of the four places by rotation: Haridwar, Prayag (Allahabad), Nasik and Ujjain.', 'http://en.wikipedia.org/wiki/Kumbh_Mela', 2),
(2, 'Taj Mahotsav 2013', '2013-02-18', '2013-02-27', 'Agra', 'Uttar Pradesh', 'img/taj-mahotsav.jpg', 'aj Mahotsav is an annual 10 day (from 18 to 27 February) event at Shilpgram in Agra, India. Every year in February tourists flock to Agra for this mega event, just a stone throw from the majestic Taj Mahal. This festival invokes the memories of old Mughal era and nawabi style prevalent in Uttar Pradesh in 18th and 19th centuries.', '', 0),
(3, 'Khajuraho Dance festival', '2013-02-01', '2013-02-08', 'Khajuraho', 'Madhya Pradesh', 'img/Khajuraho-Dance-Festival.jpg', 'One week long festival of classical dances held annually against the spectacular backdrop of the magnificently lit Khajuraho temples in Chhatarpur district of Madhya Pradesh.', '', 0),
(4, 'Holi - Festival of Colors', '2013-03-26', '2013-03-27', 'National', 'Festival', 'img/holi.jpg', 'Holi - The Festival of Colors marks the beginning of ceremonies being the first festival in the Hindu calendar. On this day, people greet each other with colours and celebrate the occasion with much gaiety and excitement.', '', 0),
(5, 'Elephant Festival', '2013-03-26', '2013-03-26', 'Jaipur', 'Rajasthan', 'img/elephant.jpg', 'Jaipur Elephant Festival, perhaps the only festival where Elephants are given prime importance.During the festival, Jaipur comes alive with elephants, dancers and musicians which draw visitors from all over the world.', '', 0),

【问题讨论】:

    标签: android bulkinsert sqliteopenhelper sqlite


    【解决方案1】:

    尝试为数据库创建一个单独的类,例如

    public class databasehandler extends SQLiteOpenHelper{
    
    private static final int DATABASE_VERSION = 5;
            // Database Name
            private static final String DATABASE_NAME = "atabase";
    
            // Contacts table name
            private static final String TABLE_Name = "task";
            // Contacts Table Columns names
            private static final String event_name= "event_name";
            private static final String start_date= "start_date";
            private static final String KEYID="id";
            private static final String  end_date= "end_date";
            private static final String event_pic= "event_pic";
            private static final String  event_desc= "event_desc";
            private static final String wiki_url= "wiki_url";
            private static final String event_cat= "event_cat";
    
    
    public databasehandler(Context context){
                    super(context, DATABASE_NAME, null, DATABASE_VERSION);
                }
    
    @Override
                public void onCreate(SQLiteDatabase db) {
                    // TODO Auto-generated method stub
                    String CREATETABLE = "CREATE TABLE " + TABLE+ "("
                            + KEYID + " INTEGER PRIMARY KEY AUTOINCREMENT," + event_name+ " TEXT,"
                            + start_date+ " TEXT," + end_date+" TEXT "+ ..... so on ")";
                                    db.execSQL(CREATE_TASK_TABLE);
                    db.execSQL(CREATETABLE );
    
                }
    
    
    
    @Override
                public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                    // Drop older table if existed
                    db.execSQL("DROP TABLE IF EXISTS " + TABLE);
                                    // Create tables again
                    onCreate(db);
    
                }
    
    void adddata(Classname Object) {
                    SQLiteDatabase db = this.getWritableDatabase();
    
                    ContentValues values = new ContentValues();
                    values.put(event_name, Object.getevent_name()); 
                    values.put(start_date, Object.gestart_date()); 
                    values.put(TASK_ID, Object.getTASK_ID());
                                            insert all values....
    
                    // Inserting Row
                    db.insert(Table_tasknotification, null, values);
                     // Closing database connection
                }
    
    }
    

    现在是 Classname.java

    public class Classname {
        //private variables name like
                  String event_name;
                  .... so on
    
            // Empty constructor
            public Classname (){
    
            }
            // constructor
            public Classname (String  event_name, String start_date,String end_date.....){
                this.event_name= event_name;
                this.start_date=start_date;
                this.end_date=end_date;
            }
    
    
            public int getID(){
                return this._id;
            }
    
            // setting id
            public void setID(int id){
                this._id = id;
            }
                       and all the get and set methods....
    
    
    
    }
    

    在你的主课中:-

    databasehandler handler=new databasehandler (context);
    handler.adddata(new Classname(event_name, start_date,end_date....));
    

    试试这个它会帮助你:) 并检查此链接http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

    【讨论】:

    • 我认为它会向数据库中添加一条记录...我有一个 sql 文件,它有一次插入 500 多条记录的查询...所以我问是否有一次插入 500 个或更多值的任何有效方法......就像 bulkinsert..
    • 我认为没有一个选项可以插入超过 500 条记录,您也必须运行 500 次查询才能插入 500 条记录......
    • 这取决于你的逻辑,你必须根据你的情况来处理它我已经上传了完整的语法,你可以很容易地实现你的逻辑
    猜你喜欢
    • 1970-01-01
    • 2013-05-07
    • 2010-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多