请教根据嵌套字典,完成组装 SQL 语句的问题。。。

2018-09-09 18:31:02 +08:00
 pppguest3962
StreamData = {
        "D1_RSET":{
            'sqltype':'int',
            'data':'61207'
        },
        "D2_DCK_time":{
            'sqltype': 'DATETIME',
            'data': '2018-9-9'
        },
        "D3_DCK_des":{
            'sqltype': 'varchar(10)',
            'data': 'TestABC'
        },
         "D4_Exp":{
            'sqltype': 'varchar(80)',
            'data': ''
        }
         "D5_stream_seq":{
            'sqltype': 'int',
            'data': '123'
        },
}

insertoTableRow = ''
insertoTableCol = ''

insertoTableSqlStr_Prefix = 'INSERT INTO %s (' % (tableName)
insertoTableSqlStr = ''

    for key1, value1 in StreamData.items():
    	#字段字句组装,这一节写不出来,如何在第一层 for 判断出第二层的 data 字段有没有内容? 有内容才把第一层的 key 加入。。。
        insertoTableRow = insertoTableRow + key1 + ',' 
        #第二层 data 字段的组装???
        for key2, value2 in StreamData[key1].items():
            if key2 == ('data'):  #应该判断 data 的 value 有没有内容的。。。。
                insertoTableCol = insertoTableCol + value + ','
                
                
完成的状态如下:

insertoTableRow = "(" + insertoTableRowSection + ")"
insertoTableCol = "(" + insertoTableColSection + ")'

#组装完成最后的 sql 语句
insertoTableSqlStr = 'INSERT INTO %s (' % (tableName) + insertoTableRow + 'value' + insertoTableCol

按照这个 StreamData 字典的例子,D4_Exp 是不用操作,因为它是空的。。。 完成组装的 SQL,写不出来。。。。冏。。。。

请教各位大大出手拉一把啊,谢谢。。。。。。

1959 次点击
所在节点    Python
7 条回复
ebingtel
2018-09-09 20:45:21 +08:00
……别写了,就算写出来,别人看着也费劲……为啥不把嵌套的 dict 先改成不嵌套的呢?
pppguest3962
2018-09-09 21:23:19 +08:00
@ebingtel,您说得很有道理,其实我今天上午也是这么想的。前面已经围绕着这个字典做了很多事情,要改也不是没办法改,只是本喵比较强迫症,看看这种情况下,是怎么样一种思路可以达到的呢。。。。^_^
xpresslink
2018-09-09 21:47:33 +08:00
弄这个真够蛋疼的,学习一下 sqlalchemy,peewee 之类的 ORM 不就得了,简单点的直接用 Django 的 ORM
wangyongbo
2018-09-09 23:05:48 +08:00
insertoTableSqlStr_Prefix = 'INSERT INTO {tableName} ({colNames}) value ({colValues})'
colNames = []
colValues = []

def f(sqltype="int", data=None, **kwargs):
if not data:
return None

if sqltype == "int":
return str(data) if data else 'null'

return "\"{}\"".format(data)

for col_name, col_value in StreamData.items():
data = f(**col_value)
if data is None:
continue
colValues.append(data)
colNames.append(col_name)

print colValues, colNames

print insertoTableSqlStr_Prefix.format(tableName='test', colNames=",".join(colNames), colValues=",".join(colValues))

这样写符合需要吗?

D4_Exp 是不用操作,因为它是空的. 空字符串和 null 还是不一样。 不一定它是个空字符串,就不需要处理。
wangyongbo
2018-09-09 23:06:32 +08:00
```

StreamData = {
"D1_RSET":{
'sqltype':'int',
'data':'61207'
},
"D2_DCK_time":{
'sqltype': 'DATETIME',
'data': '2018-9-9'
},
"D3_DCK_des":{
'sqltype': 'varchar(10)',
'data': 'TestABC'
},
"D4_Exp":{
'sqltype': 'varchar(80)',
'data': ''
},
"D5_stream_seq":{
'sqltype': 'int',
'data': '123'
},
}

insertoTableSqlStr_Prefix = 'INSERT INTO {tableName} ({colNames}) value ({colValues})'
colNames = []
colValues = []

def f(sqltype="int", data=None, **kwargs):
if not data:
return None

if sqltype == "int":
return str(data) if data else 'null'

return "\"{}\"".format(data)

for col_name, col_value in StreamData.items():
data = f(**col_value)
if data is None:
continue
colValues.append(data)
colNames.append(col_name)

print colValues, colNames

print insertoTableSqlStr_Prefix.format(tableName='test', colNames=",".join(colNames), colValues=",".join(colValues))


```
reus
2018-09-10 00:16:51 +08:00
了解过 SQL 注入吗?
csx163
2018-09-10 00:53:37 +08:00
没细看,试试 dataset 库

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/487665

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX