Di artikel kami sebelumnya – menyisipkan baris ke tabel sqlite3, kami telah melihat cara menyisipkan satu record ke tabel. Dalam tutorial ini, kita akan mempelajari cara menyisipkan beberapa baris menggunakan satu query INSERT INTO
Langkah-langkah untuk menyisipkan banyak baris ke dalam tabel sqlite3
Langkah-langkah menyisipkan banyak record ke dalam sebuah tabel adalah
- Persiapkan koneksi ke database dan kemudian dapatkan kursor
- Dapatkan semua catatan Anda ke daftar
- Gunakan metode executionmany() dan teruskan kueri dan daftar catatan sebagai argumen ke metode
Contoh 1. Masukkan Beberapa Baris ke dalam Tabel sqlite
Pada contoh berikut, kita akan menyisipkan tiga record ke dalam sebuah tabel
Program Piton
import sqlite3 conn = sqlite3.connect('mysqlite.db') c = conn.cursor() #records or rows in a list records = [(1, 'Glen', 8), (2, 'Elliot', 9), (3, 'Bob', 7)] #insert multiple records in a single query c.executemany('INSERT INTO students VALUES(?,?,?);',records); print('We have inserted', c.rowcount, 'records to the table.') #commit the changes to db conn.commit() #close the connection conn.close()Untuk memeriksa apakah kueri berhasil dijalankan dan memasukkan ketiga catatan, kami telah mencetak kursor. properti jumlah baris
Keluaran
We have inserted 3 records to the table._Ringkasan
Dalam tutorial Contoh Python ini, kami belajar memasukkan banyak catatan ke dalam tabel sqlite3 dalam satu kueri dengan bantuan contoh yang terperinci
(Sponsor) Mulailah belajar Python dengan tutorial Intro to Python gratis dari DataCamp. Pelajari Ilmu Data dengan menyelesaikan tantangan coding interaktif dan menonton video dari instruktur ahli. Mulai sekarang
Diperbarui pada 07 Januari 2020
Pernyataan sisipkan digunakan untuk menyisipkan catatan di mysql
Sintaksis. INSERT INTO() VALUES("");
Contoh 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
from __future__ import print_function import MySQLdb as my db = my.connect(host="127.0.0.1", user="root", passwd="", db="world" ) cursor = db.cursor() sql = "insert into city VALUES(null, 'Mars City', 'MAC', 'MARC', 1233)" number_of_rows = cursor.execute(sql) db.commit() # you need to call commit() method to save # your changes to the database db.close()
Program ini menyisipkan kota baru di tabel kota, perhatikan penggunaan untuk db.commit(), metode ini menyimpan perubahan Anda ke database
Contoh 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24_
from __future__ import print_function import MySQLdb as my db = my.connect(host="127.0.0.1", user="root", passwd="", db="world" ) cursor = db.cursor() name = "Some new city" country_code = 'PSE' district = 'Someyork' population = 10008 sql = "insert into city VALUES(null, '%s', '%s', '%s', %d)" % \ (name, country_code , district, population) number_of_rows = cursor.execute(sql) db.commit() db.close()
Perhatikan penggunaan karakter backslash (\) pada baris 18. Karakter \_ digunakan untuk membagi pernyataan python menjadi beberapa baris
Menyisipkan banyak baris
Untuk menyisipkan banyak baris dalam tabel, gunakan metode objek kursor from __future__ import print_function import MySQLdb as my db = my.connect(host="127.0.0.1", user="root", passwd="", db="world" ) cursor = db.cursor() sql = "insert into city VALUES(null, 'Mars City', 'MAC', 'MARC', 1233)" number_of_rows = cursor.execute(sql) db.commit() # you need to call commit() method to save # your changes to the database db.close() 0
Sintaksis. from __future__ import print_function import MySQLdb as my db = my.connect(host="127.0.0.1", user="root", passwd="", db="world" ) cursor = db.cursor() sql = "insert into city VALUES(null, 'Mars City', 'MAC', 'MARC', 1233)" number_of_rows = cursor.execute(sql) db.commit() # you need to call commit() method to save # your changes to the database db.close() 1
penyataan. string yang berisi kueri untuk dieksekusi
argumen. urutan yang berisi nilai untuk digunakan dalam pernyataan insert
Mari kita ambil contoh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34_
from __future__ import print_function import MySQLdb as my db = my.connect(host="127.0.0.1", user="root", passwd="", db="world" ) cursor = db.cursor() name = "Some new city" country_code = 'SNC' district = 'Someyork' population = 10008 data = [ ('city 1', 'MAC', 'distrct 1', 16822), ('city 2', 'PSE', 'distrct 2', 15642), ('city 3', 'ZWE', 'distrct 3', 11642), ('city 4', 'USA', 'distrct 4', 14612), ('city 5', 'USA', 'distrct 5', 17672), ] sql = "insert into city(name, countrycode, district, population) VALUES(%s, %s, %s, %s)" number_of_rows = cursor.executemany(sql, data) db.commit() db.close() _
Pada postingan berikutnya, kita akan membahas cara menangani error
Tutorial Lainnya (Sponsor)
Situs ini dengan murah hati didukung oleh DataCamp. DataCamp menawarkan Tutorial Python interaktif online untuk Ilmu Data. Bergabunglah dengan lebih dari satu juta pelajar lainnya dan mulai belajar Python untuk ilmu data hari ini