pymysql连接数据库使用各种问题

1.数据库链接

import pymysql
from sshtunnel import SSHTunnelForwarder



conn = pymysql.connect(host='****',
                           database='weibo_crawler',
                           port=3306,
                           user='root',
                           password='***')

cur = conn.cursor()
cur.execute("show databases;")
print(cur.fetchall())

# with SSHTunnelForwarder(
#         ("eng0.lavector.com", 2222),
#         ssh_username="lavector",
#         ssh_password="Lav3ctor2015",
#         # ssh_private_key_password="secret",
#         remote_bind_address=('localhost', 3306),
#         # local_bind_address=('0.0.0.0', 10022)
# ) as tunnel:
#     print("connected")
#     conn = pymysql.connect(host='127.0.0.1',
#                            database='spider_data',
#                            port=3306,
#                            user='root',
#                            password='passw0rd')
#
#     cur = conn.cursor()
#     cur.execute("show databases")
#     print(cur.fetchall())
#

import time
import datetime

print(time.time())
print(datetime.datetime)
nowTime=datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
print(nowTime)
# import paramiko
# import psycopg2
#
# from sshtunnel import SSHTunnelForwarder
# # 获取密钥
# private_key = paramiko.RSAKey.from_private_key_file('/Users/yyj/.ssh/id_rsa')
# with SSHTunnelForwarder(
#     # 指定ssh登录的跳转机的address
#     ssh_address_or_host = ('jumphost',22),
#     # 设置密钥
#     ssh_pkey = private_key,
#     # 如果是通过密码访问,可以把下面注释打开,将密钥注释即可。
#     # ssh_password = "password"
#     # 设置用户
#     ssh_username = 'username',
#     # 设置数据库服务地址及端口
#     remote_bind_address= ('dbhost',dbport)) as server:
#
#     conn = psycopg2.connect(database='dbname',
#                             user='username',
#                             password='password',
#                             host='127.0.0.1',  # 因为上面没有设置 local_bind_address,所以这里必须是127.0.0.1,如果设置了,取设置的值就行了。
#                             port=server.local_bind_port) # 这里端口也一样,上面的server可以设置,没设置取这个就行了
#
#
#     print(conn)
#
#     cur = conn.cursor()
#     # 执行查询,查看结果,验证数据库是否链接成功。
#     cur.execute("select * from t_table_data limit 1")
#
#     rows = cur.fetchone()
#
#     print(rows)
#
#     conn.close()

2.pymysql.err.ProgrammingError: 1064 (Python字符串转义问题)

里面含有引号时,可能会发生如上错误。

 

解决方法:
使用pymysql.escape_string()方法

如下:

sql = """INSERT INTO video_info(video_id, title) VALUES("%s","%s")""" % (video_info["id"],pymysql.escape_string(video_info["title"])

3.pymysql.err.DataError: 1264, "Out of range value

原因:
新版本的MySQL对字段的严格检查。
解决方法:
修改my.ini,将
sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
改为
sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"。
重新启动MySQL。
备注:

4.pymysql.err.IntegrityError: (1062, "Duplicate entry ‘2147483647’ for key 'PRIMARY

主键设为自增长不重复就行(所以插入语句主键这列可以不填,系统自动填写)

你可能感兴趣的:(知识点)