编写python程序在mysql表中插入数据fb_web_active_group_members_user_hClustering_six
if cur.execute(is_exist_table_sql) == 0:
create_sql = '''CREATE TABLE fb_web_active_group_members_user_hClustering_six (id bigint not null primary key auto_increment,userID bigint,GroupName varchar(128),
Friends int,NoProfilePhotos int,UploadPhotoSum int,NoPosts int,UploadVideoSum int,postCluster0 int,postCluster1 int,postCluster2 int,postCluster3 int,postCluster4 int,
normal_Friends float,normal_NoProfilePhotos float,normal_UploadPhotoSum float,normal_NoPosts float,normal_UploadVideoSum float,normal_postCluster0 float,
normal_postCluster1 float,normal_postCluster2 float,normal_postCluster3 float,normal_postCluster4 float,postCluster0_ratio float,postCluster1_ratio float,
postCluster2_ratio float,postCluster3_ratio float,postCluster4_ratio float, cluster_label int)'''
cur.execute(create_sql)
for item in user_level_cluster_list:
insert_sql = '''INSERT INTO fb_web_active_group_members_user_hClustering_six (userID,GroupName,
Friends,NoProfilePhotos,UploadPhotoSum,NoPosts,UploadVideoSum,postCluster0,postCluster1,postCluster2,postCluster3,postCluster4,
normal_Friends,normal_NoProfilePhotos,normal_UploadPhotoSum,normal_NoPosts,normal_UploadVideoSum,normal_postCluster0,
normal_postCluster1,normal_postCluster2,normal_postCluster3,normal_postCluster4,postCluster0_ratio,postCluster1_ratio,
postCluster2_ratio,postCluster3_ratio,postCluster4_ratio, cluster_label) VALUES
({0},"{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})'''.format(item["UserID"],
item["GroupName"],item["Friends"],item["NoProfilePhotos"],item["UploadPhotoSum"],
item["NoPosts"], item["UploadVideoSum"],item["postCluster0"],item["postCluster1"],item["postCluster2"],item["postCluster3"],
item["postCluster4"],"%.5f"% item["normal_Friends"],"%.5f"%item["normal_NoProfilePhotos"],"%.5f"%item["normal_UploadPhotoSum"],"%.5f"%item["normal_NoPosts"],
"%.5f" %item["normal_UploadVideoSum"],"%.5f"%item["normal_postCluster0"],"%.5f"%item["normal_postCluster1"],"%.5f"%item["normal_postCluster2"],
"%.5f" %item["normal_postCluster3"],"%.5f"%item["normal_postCluster4"],"%.5f"%item["postCluster0_ratio"],"%.5f"%item["postCluster1_ratio"],
"%.5f" %item["postCluster2_ratio"],"%.5f"%item["postCluster3_ratio"],"%.5f"%item["postCluster4_ratio"],item["cluster_label"])
print(insert_sql)
cur.execute(insert_sql)
但错误:
Traceback (most recent call last):
INSERT INTO fb_web_active_group_members_user_hClustering_six (userID,GroupName,Friends,NoProfilePhotos,UploadPhotoSum,NoPosts,UploadVideoSum,postCluster0,postCluster1,postCluster2,postCluster3,postCluster4,normal_Friends,normal_NoProfilePhotos,normal_UploadPhotoSum,normal_NoPosts,normal_UploadVideoSum,normal_postCluster0,normal_postCluster1,normal_postCluster2,normal_postCluster3,normal_postCluster4,postCluster0_ratio,postCluster1_ratio,postCluster2_ratio,postCluster3_ratio,postCluster4_ratio, cluster_label) VALUES (1,"Jazzmasters&Jaguars",964,195,5,339,0,119,14,73,20,36,0.19280,0.17016,0.00028,0.37184,0.00000,0.27483,0.09589,0.20055,0.18868,0.15000,0.45420,0.05344,0.27863,0.07634,0.13740,4.00000)
pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ',0.27483,0.09589,0.20055,0.18868,0.15000,0.45420,0.05344,0.27863' at line 6")
你可以告诉我原因以及如何解决它
答案 0 :(得分:0)
请使用MySQL python docs中提供的标准语法。
即。您的查询格式应为:
query = ("INSERT INTO table"
"(col1, col2, col3, col4)"
"VALUES (%s, %s, %s, %s)")
data = (value1, value2, value3, value4)
cursor.execute(query, data)
不要使用python {}
- 格式。您当前的实现(如果它碰巧工作,幸运的是,可能是由于那些奇怪的逗号)将容易受到SQL注入。