“GIS DICTIONARY A-Z” 查询页面开发(2)——INSERT INTO数据库

2022-10-14,,,,

今日工作:数据库连接、数据写入

一、数据库连接:使用了pymysql库

from g2_dataclean import deflist
import pymysql
db = pymysql.connect(db='gisdictionary',charset='utf8')
cursor = db.cursor()

二、数据写入

countid = 1 #id字段
for i in range(0,len(deflist)):
        alpha = deflist[i][1][0:1].lower() #alpha即letter字段,为单词首字母。这一步截取出首字母并.lower()小写。
        if not(alpha.isalpha()):    #判断首字母是否为a-z字母,如果不是a-z,则分配'#'给它。
            alpha = '#'
        sql = '''insert into gisaz(id,words,meaning,letter)values(%s,'%s',"%s",'%s');'''%(countid,pymysql.escape_string(deflist[i][1]),pymysql.escape_string(deflist[i][0]),alpha)
        countid += 1
        cursor.execute(sql)
try:
        db.commit()
except exception:
        print("发生错误",exception) #不会捕获mysql的warning警告,try...except只捕获error错误。
        db.rollback()    #回滚事务
finally:
        db.close()

代码部分结束,虽然今天的代码看起来又少又容易,但sql语句可真是废了不少力气。

#核心语句
#sql = '''insert into gisaz(id,words,meaning,letter)values(%s,'%s',"%s",'%s');'''
#%(countid,pymysql.escape_string(deflist[0][1]),pymysql.escape_string(deflist[0][0]),alpha)
#cursor.execute()
#db.commit()
#db.close()

#0、核心语句调试了很久,最终使用#三层引号、#%s、#pymysql.escape_string()的方式集合完成了sql语句的撰写;
#pymysql.escape_string()会将数据值string中的 单引号等有意义的符号 通过 多层转义 的方式取消对语句的影响;
#能成功的两种相同效果的无%s语句:(不必深究,下次要用时再根据这次的经验尝试即可)
#(一)'''insert into gisaz(id,words,meaning,letter)values(1,'3d feature',"a representation of a three-dimensional, 
#real-world object in a map or scene, with elevation values (z-values) stored within the feature\\'s geometry. besides geometry, 
#3d features may have attributes stored in a feature table. in applications such as cad, 3d features are often referred to 
#as 3d models.",'3');'''
#(二)'insert into gisaz(id,words,meaning,letter)values(1,\'3d feature\',"a representation of a three-dimensional, 
#real-world object in a map or scene, with elevation values (z-values) stored within the feature\\\'s geometry. besides geometry, 
#3d features may have attributes stored in a feature table. in applications such as cad, 3d features are often referred to 
#as 3d models.",\'3\');'
#参考文章:https://blog.csdn.net/qq_36622490/article/details/87455903
#1、下面这句sql删除了相同id的行数据,但是没能保留住其中id最小的那一条行数据;网上方法尝试数次未能成功,报sql语法错误:delete from cqssc where id in (select id from (select id from cqssc where expect in (select expect from cqssc group by expect having count(expect)>1) and id not in (select min(id) from cqssc group by expect having count(expect)>1)) as tmpresult)
#delete from gisaz where id in (select id from (select id from gisaz where id in (select id from gisaz group by id having count(id)>1)) as tmpresult)
#参考文章:https://www.cnblogs.com/jdbeyond/p/8157224.html
#2、已在letter列将所有首字母小写
#3、初次插入数据我选择分3次进行,犯下了重置countid的错误;要注意countid是id字段,不能重置进而引起id重复。
#4、暂时在数据表中空置了一列other,打算用其放置图片,或是为"see xxxxxx"等meaning创造通往其他词条的超链接(数据库内大概有类似功能)。
#5、(无伤大雅,meaning的数据库类型设置了为text并且没有设置长度)在phpadmin中暂时没有找到完成显示meaning数据的方法,现在都是两条如"in arcgis, a message from a replica to its relativ..."和"原始长度207"的显示;
#6、(warning!)部分words的长度超过了预设的varchar(20),造成部分词语名称不全。只得修改表结构,重新写入数据。varchar(40)也不够,应该去写一个找出deflist[n][1]最大值的程序。
#未来再做数据库还是得先确定最大长度再建表;通过简单的程序'max=len(deflist[0][1]);for i in range(0,1729):{if(len(deflist[i][1])>max):{max=len(deflist[i][1])}}'得到最大值为50,故设定varchar(60)。
#7、考虑是否要把后119条数据按照首字母重新洗入前面的数据中。

 最终数据库成果,总共1729条数据:

《“GIS DICTIONARY A-Z” 查询页面开发(2)——INSERT INTO数据库.doc》

下载本文的Word格式文档,以方便收藏与打印。