版权声明:个人随手学习笔记,任何欠妥之处,还望海涵指正~~~ https://blog.csdn.net/sinat_41842926/article/details/84797006
因本人刚开始写博客,学识经验有限,如有不正之处望读者指正,不胜感激;也望借此平台留下学习笔记以温故而知新。这个系列主要是Python数据分析基础的学习笔记。
创建数据库中的表、在表中插入数据,读取数据并对行进行计数
# 创建数据库中的表、在表中插入数据,读取数据并对行进行计数
#!/usr/bin/env python3
import sqlite3 # 轻量级的基于磁盘的数据库
# 创建SQLite3内存数据库
# 创建带有4个属性的sales表
con = sqlite3.connect(':memory:')
query = """CREATE TABLE sales
(customer VARCHAR(20),
product VARCHAR(40),
amount FLOAT,
date DATE);"""
con.execute(query)
con.commit()
# 在表中插入几行数据
data = [('Richard Lucas', 'Notepad', 2.50, '2014-01-02'),
('Jenny Kim', 'Binder', 4.15, '2014-01-15'),
('Svetlana Crow', 'Printer', 155.75, '2014-02-03'),
('Stephen Randolph', 'Computer', 679.40, '2014-02-20')]
statement = "INSERT INTO sales VALUES(?, ?, ?, ?)"
con.executemany(statement, data)
con.commit()
# 查询sales表
cursor = con.execute("SELECT * FROM sales")
rows = cursor.fetchall()
# 计算查询结果中行的数量
row_counter = 0
for row in rows:
print(row)
row_counter += 1
print('Number of rows: %d' % (row_counter))
('Richard Lucas', 'Notepad', 2.5, '2014-01-02')
('Jenny Kim', 'Binder', 4.15, '2014-01-15')
('Svetlana Crow', 'Printer', 155.75, '2014-02-03')
('Stephen Randolph', 'Computer', 679.4, '2014-02-20')
Number of rows: 4
用CSV文件向表中添加数据和更新表中数据
# 用CSV文件向表中添加数据和更新表中数据
#!/usr/bin/env python3
import csv
import sqlite3
import sys
# CSV输入文件的路径和文件名
input_file = sys.argv[1]
# 创建SQLite3内存数据库
# 创建带有5个属性的Suppliers表
con = sqlite3.connect('Suppliers.db')
c = con.cursor()
create_table = """CREATE TABLE IF NOT EXISTS Suppliers
(Supplier_Name VARCHAR(20),
Invoice_Number VARCHAR(20),
Part_Number VARCHAR(20),
Cost FLOAT,
Purchase_Date DATE);"""
c.execute(create_table)
con.commit()
# 读取CSV文件
# 向Suppliers表中插入数据
file_reader = csv.reader(open(input_file, 'r'), delimiter=',')
header = next(file_reader, None)
for row in file_reader:
data = []
for column_index in range(len(header)):
data.append(row[column_index])
print(data)
c.execute("INSERT INTO Suppliers VALUES (?, ?, ?, ?, ?);", data)
con.commit()
print('')
# 查询Suppliers表
output = c.execute("SELECT * FROM Suppliers")
rows = output.fetchall()
for row in rows:
output = []
for column_index in range(len(row)):
output.append(str(row[column_index]))
print(output)
C:\Users\yuxi\Desktop>python 1db_count_rows.py supplier_data.csv
# 插入数据
['Supplier X', '001-1001', '2341', '$500.00', '1/20/14']
['Supplier X', '001-1001', '2341', '$500.00', '1/20/14']
['Supplier X', '001-1001', '5467', '$750.00', '1/20/14']
['Supplier X', '001-1001', '5467', '$750.00', '1/20/14']
['Supplier Y', '50-9501', '7009', '$250.00', '1/30/14']
['Supplier Y', '50-9501', '7009', '$250.00', '1/30/14']
['Supplier Y', '50-9505', '6650', '$125.00', '2/3/14']
['Supplier Y', '50-9505', '6650', '$125.00', '2/3/14']
['Supplier Z', '920-4803', '3321', '$615.00', '2/3/14']
['Supplier Z', '920-4804', '3321', '$615.00', '2/10/14']
['Supplier Z', '920-4805', '3321', '$615.00', '2/17/14']
['Supplier Z', '920-4806', '3321', '$615.00', '2/24/14']
# 提取数据
['Supplier X', '001-1001', '2341', '$500.00', '1/20/14']
['Supplier X', '001-1001', '2341', '$500.00', '1/20/14']
['Supplier X', '001-1001', '5467', '$750.00', '1/20/14']
['Supplier X', '001-1001', '5467', '$750.00', '1/20/14']
['Supplier Y', '50-9501', '7009', '$250.00', '1/30/14']
['Supplier Y', '50-9501', '7009', '$250.00', '1/30/14']
['Supplier Y', '50-9505', '6650', '$125.00', '2/3/14']
['Supplier Y', '50-9505', '6650', '$125.00', '2/3/14']
['Supplier Z', '920-4803', '3321', '$615.00', '2/3/14']
['Supplier Z', '920-4804', '3321', '$615.00', '2/10/14']
['Supplier Z', '920-4805', '3321', '$615.00', '2/17/14']
['Supplier Z', '920-4806', '3321', '$615.00', '2/24/14']
使用CSV输入文件更新数据表中已有的记录
# 使用CSV输入文件更新数据表中已有的记录
#!/usr/bin/env python3
import csv
import sqlite3
import sys
# CSV输入文件的路径和文件名
input_file = sys.argv[1]
# 创建SQLite3内存数据库
# 创建带有4个属性的sales表
con = sqlite3.connect(':memory:')
query = """CREATE TABLE IF NOT EXISTS sales
(customer VARCHAR(20),
product VARCHAR(40),
amount FLOAT,
date DATE);"""
con.execute(query)
con.commit()
# 向表中插入几行数据
data = [('Richard Lucas', 'Notepad', 2.50, '2014-01-02'),
('Jenny Kim', 'Binder', 4.15, '2014-01-15'),
('Svetlana Crow', 'Printer', 155.75, '2014-02-03'),
('Stephen Randolph', 'Computer', 679.40, '2014-02-20')]
for tuple in data:
print(tuple)
statement = "INSERT INTO sales VALUES(?, ?, ?, ?)"
con.executemany(statement, data)
con.commit()
print('')
# 读取CSV文件并更新特定的行
file_reader = csv.reader(open(input_file, 'r'), delimiter=',')
header = next(file_reader, None)
for row in file_reader:
data = []
for column_index in range(len(header)):
data.append(row[column_index])
print(data)
con.execute("UPDATE sales SET amount=?, date=? WHERE customer=?;", data)
con.commit()
print('')
# 查询sales表
cursor = con.execute("SELECT * FROM sales")
rows = cursor.fetchall()
for row in rows:
output = []
for column_index in range(len(row)):
output.append(str(row[column_index]))
print(output)
C:\Users\yuxi\Desktop>python 1db_count_rows.py data_for_updating.csv
# 插入数据
('Richard Lucas', 'Notepad', 2.5, '2014-01-02')
('Jenny Kim', 'Binder', 4.15, '2014-01-15')
('Svetlana Crow', 'Printer', 155.75, '2014-02-03')
('Stephen Randolph', 'Computer', 679.4, '2014-02-20')
# 要更新到数据库中的数据
['4.25', '5/11/2014', 'Richard Lucas']
['6.75', '5/12/2014', 'Jenny Kim']
# 提取数据
['Richard Lucas', 'Notepad', '4.25', '5/11/2014']
['Jenny Kim', 'Binder', '6.75', '5/12/2014']
['Svetlana Crow', 'Printer', '155.75', '2014-02-03']
['Stephen Randolph', 'Computer', '679.4', '2014-02-20']
创建一个数据库和用来保存数据的数据表
Enter password: ****
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 48
Server version: 5.1.16-beta-community-nt MySQL Community Server (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| my_supplier |
| mysql |
| test |
+--------------------+
4 rows in set (0.01 sec)
mysql> create database my_suppliers;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| my_supplier |
| my_suppliers |
| mysql |
| test |
+--------------------+
5 rows in set (0.00 sec)
mysql> use my_suppliers;
Database changed
mysql> create table if not exists Suppliers
-> (Supplier_Name VARCHAR(20),Invoice_Number VARCHAR(20),Part_Number VARCHAR(20),Cost FLOAT,Purchase_Date DATE);
Query OK, 0 rows affected (0.08 sec)
mysql> describe Suppliers;
+----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+-------+
| Supplier_Name | varchar(20) | YES | | NULL | |
| Invoice_Number | varchar(20) | YES | | NULL | |
| Part_Number | varchar(20) | YES | | NULL | |
| Cost | float | YES | | NULL | |
| Purchase_Date | date | YES | | NULL | |
+----------------+-------------+------+-----+---------+-------+
5 rows in set (0.01 sec)
mysql> create user 'yuxi'@'localhost'identified by '1111';
Query OK, 0 rows affected (0.09 sec)
mysql> grant all privileges on my_suppliers.* to 'yuxi'@'localhost';
Query OK, 0 rows affected (0.01 sec)
数据从CSV文件中插入到数据表并展示表中的数据
# 数据从CSV文件中插入到数据表,然后展示表中的数据
import csv
import sys
import MySQLdb
from datetime import datetime
# CSV输入文件的路径和文件名
input_file = sys.argv[1]
# 连接MySQL数据库
con = MySQLdb.connect(host='localhost',port=3306,db='my_suppliers',\
user='yuxi',passwd='1111')
c = con.cursor()
# 向Suppliers表中插入数据
file_reader = csv.reader(open(input_file,'r',newline=''))
header = next(file_reader)
for row in file_reader:
data = []
for column_index in range(len(header)):
if column_index < 4:
data.append(str(row[column_index]).lstrip('$').replace\
(',','').strip())
else:
a_data = datetime.date(datetime.strptime(\
str(row[column_index]),'%m/%d/%y'))
a_data = a_data.strftime('%Y-%m-%d')
data.append(a_data)
print(data)
c.execute('INSERT INTO Suppliers VALUES(%s,%s,%s,%s,%s);',data)
con.commit()
print('')
# 查询Suppliers表
c.execute('SELECT * FROM Suppliers')
rows = c.fetchall()
for row in rows:
row_list_output = []
for column_index in range(len(row)):
row_list_output.append(row[column_index])
print(row_list_output)
C:\Users\yuxi\Desktop>Python 1db_count_rows.py supplier_data.csv
# 插入数据
['Supplier X', '001-1001', '2341', '500.00', '2014-01-20']
['Supplier X', '001-1001', '2341', '500.00', '2014-01-20']
['Supplier X', '001-1001', '5467', '750.00', '2014-01-20']
['Supplier X', '001-1001', '5467', '750.00', '2014-01-20']
['Supplier Y', '50-9501', '7009', '250.00', '2014-01-30']
['Supplier Y', '50-9501', '7009', '250.00', '2014-01-30']
['Supplier Y', '50-9505', '6650', '125.00', '2014-02-03']
['Supplier Y', '50-9505', '6650', '125.00', '2014-02-03']
['Supplier Z', '920-4803', '3321', '615.00', '2014-02-03']
['Supplier Z', '920-4804', '3321', '615.00', '2014-02-10']
['Supplier Z', '920-4805', '3321', '615.00', '2014-02-17']
['Supplier Z', '920-4806', '3321', '615.00', '2014-02-24']
# 查询数据
['Supplier X', '001-1001', '2341', 500.0, datetime.date(2014, 1, 20)]
['Supplier X', '001-1001', '2341', 500.0, datetime.date(2014, 1, 20)]
['Supplier X', '001-1001', '5467', 750.0, datetime.date(2014, 1, 20)]
['Supplier X', '001-1001', '5467', 750.0, datetime.date(2014, 1, 20)]
['Supplier Y', '50-9501', '7009', 250.0, datetime.date(2014, 1, 30)]
['Supplier Y', '50-9501', '7009', 250.0, datetime.date(2014, 1, 30)]
['Supplier Y', '50-9505', '6650', 125.0, datetime.date(2014, 2, 3)]
['Supplier Y', '50-9505', '6650', 125.0, datetime.date(2014, 2, 3)]
['Supplier Z', '920-4803', '3321', 615.0, datetime.date(2014, 2, 3)]
['Supplier Z', '920-4804', '3321', 615.0, datetime.date(2014, 2, 10)]
['Supplier Z', '920-4805', '3321', 615.0, datetime.date(2014, 2, 17)]
['Supplier Z', '920-4806', '3321', 615.0, datetime.date(2014, 2, 24)]
从Suppliers数据表中查询出一组特定记录数据并输出写入到CSV输出文件
# 从Suppliers 数据表中查询出一组特定记录,然后将输出写入CSV 输出文件
import sys
import csv
import MySQLdb
# CSV文件的输出路径和文件名
output_file = sys.argv[1]
# 连接MySQL数据库
con = MySQLdb.connect(host='localhost',port=3306,db='my_suppliers',\
user='yuxi',passwd='1111')
c = con.cursor()
# c创建写文件的对象,并写入标题行
filewriter = csv.writer(open(output_file,'w',newline=''),delimiter=',')
header = ['Supplier Name','Invoice Number','Part Number','Cost',\
'Purchase Date']
filewriter.writerow(header)
# 查询Suppliers表,并将结果写入CSV输出文件
c.execute('SELECT * FROM Suppliers WHERE Cost>700.0;')
rows = c.fetchall()
for row in rows:
filewriter.writerow(row)
Supplier Name,Invoice Number,Part Number,Cost,Purchase Date
Supplier X,001-1001,5467,750.0,2014-01-20
Supplier X,001-1001,5467,750.0,2014-01-20
Supplier X,001-1001,5467,750.0,2014-01-20
Supplier X,001-1001,5467,750.0,2014-01-20
使用CSV输入文件更新MySQL 数据表中已有的记录
# 使用CSV输入文件更新MySQL 数据表中已有的记录
import sys
import MySQLdb
import csv
# CSV输入文件的路径和文件名
input_file = sys.argv[1]
con = MySQLdb.connect(host='localhost',port=3306,db='my_suppliers',\
user='yuxi',passwd='1111')
c = con.cursor()
# 读取CSV文件并更新特定的行
file_reader = csv.reader(open(input_file,'r',newline=''),delimiter=',')
header = next(file_reader)
for row in file_reader:
data = []
for column_index in range(len(header)):
data.append(str(row[column_index]).strip())
print(data)
c.execute('''UPDATE Suppliers SET Cost=%s,\
Purchase_Date=%s WHERE Supplier_Name=%s;''',data)
con.commit()
print('')
# 查询Suppliers表
c.execute('SELECT * FROM Suppliers')
rows = c.fetchall()
for row in rows:
output = []
for column_index in range(len(row)):
output.append(str(row[column_index]))
print(output)
C:\Users\yuxi\Desktop>Python 1db_count_rows.py data_for_updating_mysql.csv
# CSV 文件中的数据
['600.00', '2014-01-22', 'Supplier X']
['200.00', '2014-02-01', 'Supplier Y']
# 记录更新之后数据表中的数据
['Supplier X', '001-1001', '2341', '600.0', '2014-01-22']
['Supplier X', '001-1001', '2341', '600.0', '2014-01-22']
['Supplier X', '001-1001', '5467', '600.0', '2014-01-22']
['Supplier X', '001-1001', '5467', '600.0', '2014-01-22']
['Supplier Y', '50-9501', '7009', '200.0', '2014-02-01']
['Supplier Y', '50-9501', '7009', '200.0', '2014-02-01']
['Supplier Y', '50-9505', '6650', '200.0', '2014-02-01']
['Supplier Y', '50-9505', '6650', '200.0', '2014-02-01']
['Supplier Z', '920-4803', '3321', '615.0', '2014-02-03']
['Supplier Z', '920-4804', '3321', '615.0', '2014-02-10']
['Supplier Z', '920-4805', '3321', '615.0', '2014-02-17']
['Supplier Z', '920-4806', '3321', '615.0', '2014-02-24']