Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Approach:
- Bàn luận
- Trong bài viết này, chúng tôi sẽ thảo luận về cách nhập nội dung tệp CSV vào bảng cơ sở dữ liệu SQLite bằng Python.
- Lúc đầu, chúng tôi nhập mô -đun CSV [để làm việc với tệp CSV] và mô -đun SQLite3 [để điền vào bảng cơ sở dữ liệu].
- Sau đó, chúng tôi kết nối với cơ sở dữ liệu Geek của chúng tôi bằng phương thức SQLite3.connect [].
- Tại thời điểm này, chúng tôi tạo một đối tượng con trỏ để xử lý các truy vấn trên bảng cơ sở dữ liệu.
- Trước tiên chúng tôi tạo bảng người của chúng tôi và tạo một tệp CSV với các nội dung bên trong mà chúng tôi sẽ chèn vào bảng của chúng tôi.
- Chúng tôi mở tệp CSV được tạo ở trên bằng hàm Open [].
- Chúng tôi trích xuất tất cả các nội dung của tệp CSV vào biến nội dung của chúng tôi thông qua phương thức csv.Reader [].
Sau đó, chúng tôi chèn nội dung hàng theo hàng của tệp CSV vào cơ sở dữ liệu của chúng tôi thông qua phương thức EXECUTEMANY [] sẽ thay thế [?,?] Với hai dữ liệu được phân tách bằng dấu phẩy tiếp theo của tệp CSV và chèn nó dưới dạng bản ghi vào bảng người.
Cuối cùng, chúng tôi xác minh rằng dữ liệu của tệp CSV đã được chèn thành công vào bảng của chúng tôi với câu lệnh CHỌN và cam kết các thay đổi và đóng kết nối cơ sở dữ liệu.
Dưới đây là việc thực hiện.
Python3
Với mục đích thực hiện, chúng tôi sẽ tạo một bảng một người trong cơ sở dữ liệu Geek.DB của chúng tôi. Chúng tôi sẽ chèn nội dung của person_records.csv trong bảng cá nhân của chúng tôi. Dưới đây là tệp CSV chúng tôi sẽ sử dụng:
Dưới đây là chương trình hoàn chỉnh dựa trên phương pháp trên:
import
csv
import
sqlite3
cursor.executemany["insert into student [name, age] VALUES [?, ?];", student_info]4
=
cursor.executemany["insert into student [name, age] VALUES [?, ?];", student_info]6
connection
=
sqlite3.connect[
'g4g.db'
____10
cursor.executemany["insert into student [name, age] VALUES [?, ?];", student_info]1
=
cursor.executemany["insert into student [name, age] VALUES [?, ?];", student_info]3
cursor.executemany["insert into student [name, age] VALUES [?, ?];", student_info]7
=
cursor.executemany["insert into student [name, age] VALUES [?, ?];", student_info]9
import
0import
1cursor.executemany["insert into student [name, age] VALUES [?, ?];", student_info]0
csv
1
import
3=
import
5
cursor.executemany["insert into student [name, age] VALUES [?, ?];", student_info]7
cursor.executemany["insert into student [name, age] VALUES [?, ?];", student_info]0
import
8=
csv
0
csv
2=
csv
4
import
2import
3import
4
import
5
import
6
Output:
SQLite:
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luận
Approach:
- Trong bài viết này, chúng tôi sẽ học cách nhập dữ liệu từ tệp CSV và lưu trữ nó trong một bảng trong cơ sở dữ liệu SQLite bằng Python. Bạn có thể tải xuống tệp CSV từ đây có chứa dữ liệu mẫu về tên và tuổi của một vài học sinh.
- Nội dung của tệp CSV
- Nhập các mô -đun cần thiết
sqliteConnection = sqlite3.connect['sql.db'] cursor = sqliteConnection.cursor[]
- Đọc dữ liệu từ DicTreader tệp CSV []
- Thiết lập kết nối với cơ sở dữ liệu.
cursor.executemany["insert into student [name, age] VALUES [?, ?];", student_info]
- Tạo bảng sinh viên và thực thi truy vấn bằng phương thức Execute [].
- Chèn dữ liệu vào bảng
Đọc dữ liệu từ bảng
Python3
Và đóng cơ sở dữ liệu.
Dưới đây là việc thực hiện:
sqlite3
1sqlite3
2
import
2sqlite3
4
cursor.executemany["insert into student [name, age] VALUES [?, ?];", student_info]9
import
0sqlite3
7sqlite3
8sqlite3
9connection
0import
csv
import
sqlite3
connection
1import
3=
9
connection
1connection
2=
connection
4
import
2
cursor.executemany["insert into student [name, age] VALUES [?, ?];", student_info]1
=
sqlite3.connect[
9import
2'g4g.db'
1'g4g.db'
2
cursor.executemany["insert into student [name, age] VALUES [?, ?];", student_info]0
import
2'g4g.db'
5
connection
1'g4g.db'
7'g4g.db'
8
import
2'g4g.db'
1
cursor.executemany["insert into student [name, age] VALUES [?, ?];", student_info]01
cursor.executemany["insert into student [name, age] VALUES [?, ?];", student_info]0
import
2
cursor.executemany["insert into student [name, age] VALUES [?, ?];", student_info]04
=
cursor.executemany["insert into student [name, age] VALUES [?, ?];", student_info]06
import
2import
3
cursor.executemany["insert into student [name, age] VALUES [?, ?];", student_info]09
import
2
cursor.executemany["insert into student [name, age] VALUES [?, ?];", student_info]11
import
2
cursor.executemany["insert into student [name, age] VALUES [?, ?];", student_info]13
cursor.executemany["insert into student [name, age] VALUES [?, ?];", student_info]14
cursor.executemany["insert into student [name, age] VALUES [?, ?];", student_info]15
import
2import
3import
0
cursor.executemany["insert into student [name, age] VALUES [?, ?];", student_info]19
cursor.executemany["insert into student [name, age] VALUES [?, ?];", student_info]20
cursor.executemany["insert into student [name, age] VALUES [?, ?];", student_info]21
sqlite3
2import
2
cursor.executemany["insert into student [name, age] VALUES [?, ?];", student_info]24
cursor.executemany["insert into student [name, age] VALUES [?, ?];", student_info]25
connection
1
cursor.executemany["insert into student [name, age] VALUES [?, ?];", student_info]27
connection
1import
3import
0
cursor.executemany["insert into student [name, age] VALUES [?, ?];", student_info]31
cursor.executemany["insert into student [name, age] VALUES [?, ?];", student_info]0
Output: