import pymysql
#建立连接
# conn = pymysql.connect( # 创建数据库连接
# host='localhost', # 要连接的数据库所在主机ip
# user='root', # 数据库登录用户名
# password='66666666', # 登录用户密码
# charset='utf8' # 编码,注意不能写成utf-8
# )
#创建光标
# cursor = conn.cursor()
#创建数据库
# cursor.execute("create database comment_db character set utf8;") 数据库叫comment_db
#创建表格
# cursor.execute("create table comments(comment char(255))character set utf8;") #表格叫comments
# conn.commit()
#写入
# sql="""insert into comments(comment) values ("哈哈哈哈哈哈哈")"""
# cursor.execute(sql)
# conn.commit()
#删除
# cursor.execute("drop table if exists comments") #如果new_database数据库存在则删除
# conn.commit()
#从表格中选择内容打印
# cursor.execute('select * from comments;')
# str = cursor.fetchall()
# print(str)
# cursor.close()
# conn.close()
# # 执行完之后别忘了关闭游标和数据库连接
# cursor.close()
# conn.close()
#csv写入
# csv_reader = csv.reader(open('/Users/liubo/PycharmProjects/pythonProject1/TEST_2/B_comment.csv', encoding='utf8'))
# for row in csv_reader:
# sql = 'insert into comments(comment) values(%s)'
# args = (row.__str__())
# num = cursor.execute(sql, args=args)
# conn.commit()
|