久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

      • <bdo id='Pw2mc'></bdo><ul id='Pw2mc'></ul>

      <small id='Pw2mc'></small><noframes id='Pw2mc'>

      <tfoot id='Pw2mc'></tfoot>

      1. <legend id='Pw2mc'><style id='Pw2mc'><dir id='Pw2mc'><q id='Pw2mc'></q></dir></style></legend>
        <i id='Pw2mc'><tr id='Pw2mc'><dt id='Pw2mc'><q id='Pw2mc'><span id='Pw2mc'><b id='Pw2mc'><form id='Pw2mc'><ins id='Pw2mc'></ins><ul id='Pw2mc'></ul><sub id='Pw2mc'></sub></form><legend id='Pw2mc'></legend><bdo id='Pw2mc'><pre id='Pw2mc'><center id='Pw2mc'></center></pre></bdo></b><th id='Pw2mc'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='Pw2mc'><tfoot id='Pw2mc'></tfoot><dl id='Pw2mc'><fieldset id='Pw2mc'></fieldset></dl></div>

        使用 psycopg2 python 庫并使用良好的轉換類型工具構

        build SQL dynamic query with psycopg2 python library and using good conversion type tools(使用 psycopg2 python 庫并使用良好的轉換類型工具構建 SQL 動態查詢)

          • <legend id='RIbj6'><style id='RIbj6'><dir id='RIbj6'><q id='RIbj6'></q></dir></style></legend>
            <tfoot id='RIbj6'></tfoot>
              • <bdo id='RIbj6'></bdo><ul id='RIbj6'></ul>

                    <tbody id='RIbj6'></tbody>

                  <small id='RIbj6'></small><noframes id='RIbj6'>

                • <i id='RIbj6'><tr id='RIbj6'><dt id='RIbj6'><q id='RIbj6'><span id='RIbj6'><b id='RIbj6'><form id='RIbj6'><ins id='RIbj6'></ins><ul id='RIbj6'></ul><sub id='RIbj6'></sub></form><legend id='RIbj6'></legend><bdo id='RIbj6'><pre id='RIbj6'><center id='RIbj6'></center></pre></bdo></b><th id='RIbj6'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='RIbj6'><tfoot id='RIbj6'></tfoot><dl id='RIbj6'><fieldset id='RIbj6'></fieldset></dl></div>
                  本文介紹了使用 psycopg2 python 庫并使用良好的轉換類型工具構建 SQL 動態查詢的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我在設計一個使用 psycopg2 庫規范的好算法時遇到了一些問題 這里

                  I have some problem to design a good algorithm which use specification of psycopg2 library described here

                  我想建立一個與這個字符串相等的動態查詢:

                  I want to build a dynamic query equal to this string :

                  SELECT ST_GeomFromText('POLYGON((0.0 0.0,20.0 0.0,20.0 20.0,0.0 20.0,0.0 0.0))');
                  

                  如您所見,我的 POLYGON 對象包含多個點,讀取一個簡單的 csv 文件 some.csv,其中包含:

                  As you can see, my POLYGON object contain multiple point, read in a simple csv file some.csv which contain :

                  0.0;0.0
                  20.0;0.0
                  20.0;20.0
                  0.0;20.0
                  0.0;0.0
                  

                  所以我動態構建查詢,csv 中行/數據數的函數.

                  So i build the query dynamically, function of the number of line/data in the csv.

                  這里我的程序生成要執行的 SQL Query 字符串:

                  Here my program to generate the SQL Query string to execute :

                  import psycopg2
                  import csv 
                  
                  # list of points
                  lXy = []
                  
                  DSN= "dbname='testS' user='postgres' password='postgres' host='localhost'"
                  conn = psycopg2.connect(DSN)
                  
                  curs = conn.cursor()
                  
                  def genPointText(curs,x,y):
                      generatedPoint = "%s %s" % (x,y)
                      return generatedPoint
                  
                  #Lecture fichier csv
                  polygonFile = open('some.csv', 'rb')
                  readerCSV = csv.reader(polygonFile,delimiter = ';')
                  
                  for coordinates in readerCSV:
                      lXy.append(genPointText(curs,float(coordinates[0]),float(coordinates[1])))
                  
                  # function of list concatenation by separator
                  def convert(myList,separator):
                      return separator.join([str(i) for i in myList])
                  
                  # construct simple query with psycopg
                  def genPolygonText(curs,l):
                      # http://initd.org/psycopg/docs/usage.html#python-types-adaptation
                      generatedPolygon = "POLYGON((%s))" % convert(l, ",")
                      return generatedPolygon
                  
                  def executeWKT(curs,geomObject,srid):
                      try:
                              # geometry ST_GeomFromText(text WKT, integer srid);
                          finalWKT = "SELECT ST_GeomFromText('%s');" % (geomObject) 
                          print finalWKT
                          curs.execute(finalWKT)
                      except psycopg2.ProgrammingError,err:
                          print "ERROR = " , err
                  
                  polygonQuery = genPolygonText(curs,lXy)
                  executeWKT(curs,polygonQuery,4326)
                  

                  如您所見,這是可行的,但是由于python對象和sql postgresql對象之間的轉換問題,這種方式不正確.

                  As you can see, that's works, but this way is not correct because of conversion problem between python object and sql postgresql object.

                  在文檔中,我只看到了為靜態查詢提供和轉換數據的示例.您是否知道在查詢的動態構建中創建具有正確類型的正確字符串的優雅"方式?

                  In the documentation, i see only example to feed and convert data for static query. Do you know an "elegant" way to create correct string with correct type in a dynamic build for query ?

                  更新 1:

                  如你所見,當我在這個簡單的例子中使用 psycopg 類型轉換函數時,我有這樣的錯誤:

                  As you can see, when i use psycopg type transformation function on this simple example, i have error like this :

                  <代碼>查詢= ST_GeomFromText( 'POLYGON((52.146542 19.050557,52.148430 19.045527,52.149525 19.045831,52.147400 19.050780,52.147400 19.050780,52.146542 19.050557))',4326)"名稱 = 我的表"嘗試:curs.execute('INSERT INTO %s(name, url, id, point_geom, poly_geom) VALUES (%s);', (name,query))除了 psycopg2.ProgrammingError,錯誤:打印錯誤=",錯誤

                  錯誤相等:

                  ERROR =  ERREUR:  erreur de syntaxe sur ou près de ? E'my_table' ?
                  LINE 1: INSERT INTO E'my_table'(name, poly_geom) VALUES (E'ST_GeomFr...
                  

                  更新 2:

                  感謝stackoverflow用戶的最終代碼!

                  Final code which work thanks to stackoverflow users !

                  #info lib : http://www.initd.org/psycopg/docs/
                  import psycopg2
                  # info lib : http://docs.python.org/2/library/csv.html
                  import csv 
                  
                  # list of points
                  lXy = []
                  
                  DSN= "dbname='testS' user='postgres' password='postgres' host='localhost'"
                  
                  print "Opening connection using dns:", DSN
                  conn = psycopg2.connect(DSN)
                  
                  curs = conn.cursor()
                  
                  def genPointText(curs,x,y):
                      generatedPoint = "%s %s" % (x,y)
                      return generatedPoint
                  
                  #Lecture fichier csv
                  polygonFile = open('some.csv', 'rb')
                  readerCSV = csv.reader(polygonFile,delimiter = ';')
                  
                  for coordinates in readerCSV:
                      lXy.append(genPointText(curs,float(coordinates[0]),float(coordinates[1])))
                  
                  # function of list concatenation by separator
                  def convert(myList,separator):
                      return separator.join([str(i) for i in myList])
                  
                  # construct simple query with psycopg
                  def genPolygonText(l):
                      # http://initd.org/psycopg/docs/usage.html#python-types-adaptation
                      generatedPolygon = "POLYGON((%s))" % convert(l, ",")
                      return generatedPolygon
                  
                  def generateInsert(curs,tableName,name,geomObject):
                      curs.execute('INSERT INTO binome1(name,geom) VALUES (%s, %s);' , (name,geomObject))
                  
                  
                  def create_db_binome(conn,name):
                  
                      curs = conn.cursor()
                  
                      SQL = (
                          "CREATE TABLE %s"
                          " ("
                          " polyname character varying(15),"
                          " geom geometry,"
                          " id serial NOT NULL,"
                          " CONSTRAINT id_key PRIMARY KEY (id)"
                          " )" 
                          " WITH ("
                          " OIDS=FALSE"
                          " );"
                          " ALTER TABLE %s OWNER TO postgres;"
                          ) %(name,name)
                      try:
                        #print SQL
                        curs.execute(SQL)
                  
                      except psycopg2.ProgrammingError,err:
                        conn.rollback()
                        dropQuery = "ALTER TABLE %s DROP CONSTRAINT id_key; DROP TABLE %s;" % (name,name)
                        curs.execute(dropQuery)
                        curs.execute(SQL)
                  
                      conn.commit()
                  
                  def insert_geometry(polyname,tablename,geometry):
                  
                      escaped_name = tablename.replace('""','""')
                  
                      try:
                          test = 'INSERT INTO %s(polyname, geom) VALUES(%%s, ST_GeomFromText(%%s,%%s))' % (escaped_name)
                          curs.execute(test, (tablename, geometry, 4326))
                          conn.commit()
                      except psycopg2.ProgrammingError,err:
                          print "ERROR = " , err
                  
                  ################
                  # PROGRAM MAIN #
                  ################
                  
                  polygonQuery = genPolygonText(lXy)
                  srid = 4326
                  table = "binome1"
                  
                  create_db_binome(conn,table)
                  insert_geometry("Berlin",table,polygonQuery)
                  insert_geometry("Paris",table,polygonQuery)
                  
                  polygonFile.close()
                  conn.close()
                  

                  推薦答案

                  您正在嘗試將表名作為參數傳遞.如果您只是查看 PostgreSQL 錯誤日志,您可能會立即看到這一點.

                  You are trying to pass a table name as a parameter. You probably could've seen this immediately if you'd just looked at the PostgreSQL error log.

                  您試圖通過 psycopg2 作為參數傳遞的表名被轉義,產生如下查詢:

                  The table name you're trying to pass through psycopg2 as a parameter is being escaped, producing a query like:

                  INSERT INTO E'my_table'(name, url, id, point_geom, poly_geom) VALUES (E'ST_GeomFromText(''POLYGON(( 52.146542 19.050557, 52.148430 19.045527, 52.149525 19.045831, 52.147400 19.050780, 52.147400 19.050780, 52.146542 19.050557))'',4326)');'
                  

                  這不是您想要的,也不會起作用;你不能像文字一樣轉義表名.必須使用普通的 Python 字符串插值來構造動態 SQL,對于實際字面值,只能使用參數化語句占位符.

                  This isn't what you intended and won't work; you can't escape a table name like a literal. You must use normal Python string interpolation to construct dynamic SQL, you can only use parameterized statement placeholders for actual literal values.

                  params = ('POLYGON(( 52.146542 19.050557, 52.148430 19.045527, 52.149525 19.045831, 52.147400 19.050780, 52.147400 19.050780, 52.146542 19.050557))',4326)
                  escaped_name = name.replace('"",'""')
                  curs.execute('INSERT INTO "%s"(name, url, id, point_geom, poly_geom) VALUES (ST_GeomFromText(%%s,%%s));' % escaped_name, params)
                  

                  看看我是如何直接插入名稱來生成查詢字符串的:

                  See how I've interpolated the name directly to produce the query string:

                  INSERT INTO my_table(name, url, id, point_geom, poly_geom) VALUES (ST_GeomFromText(%s,%s));
                  

                  (%% 通過 % 替換轉換為純 %).然后我將該查詢與定義 POLYGON 的字符串和 ST_GeomFromText 的另一個參數作為查詢參數一起使用.

                  (%% gets converted to plain % by % substitution). Then I'm using that query with the string defining the POLYGON and the other argument to ST_GeomFromText as query parameters.

                  我沒有對此進行測試,但它應該可以為您提供正確的想法并幫助您解釋問題所在.

                  I haven't tested this, but it should give you the right idea and help explain what's wrong.

                  非常小心在進行這樣的字符串插值時,這是 的簡單途徑SQL 注入.我在上面顯示的代碼中做了非常粗略的引用,但是如果您的客戶端庫提供了一個正確的標識符引用函數,我想使用它.

                  BE EXTEMELY CAREFUL when doing string interpolation like this, it's an easy avenue for SQL injection. I've done very crude quoting in the code shown above, but I'd want to use a proper identifier quoting function if your client library offers one.

                  這篇關于使用 psycopg2 python 庫并使用良好的轉換類型工具構建 SQL 動態查詢的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  How to bind a function to an Action from Qt menubar?(如何將函數綁定到 Qt 菜單欄中的操作?)
                  PyQt progress jumps to 100% after it starts(PyQt 啟動后進度躍升至 100%)
                  How to set yaxis tick label in a fixed position so that when i scroll left or right the yaxis tick label should be visible?(如何將 yaxis 刻度標簽設置在固定位置,以便當我向左或向右滾動時,yaxis 刻度標簽應該可見
                  `QImage` constructor has unknown keyword `data`(`QImage` 構造函數有未知關鍵字 `data`)
                  Change x-axis ticks to custom strings(將 x 軸刻度更改為自定義字符串)
                  How to show progress bar while saving file to excel in python?(如何在python中將文件保存為excel時顯示進度條?)
                  <i id='4haKL'><tr id='4haKL'><dt id='4haKL'><q id='4haKL'><span id='4haKL'><b id='4haKL'><form id='4haKL'><ins id='4haKL'></ins><ul id='4haKL'></ul><sub id='4haKL'></sub></form><legend id='4haKL'></legend><bdo id='4haKL'><pre id='4haKL'><center id='4haKL'></center></pre></bdo></b><th id='4haKL'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='4haKL'><tfoot id='4haKL'></tfoot><dl id='4haKL'><fieldset id='4haKL'></fieldset></dl></div>
                  1. <small id='4haKL'></small><noframes id='4haKL'>

                    • <tfoot id='4haKL'></tfoot>
                        <bdo id='4haKL'></bdo><ul id='4haKL'></ul>
                        <legend id='4haKL'><style id='4haKL'><dir id='4haKL'><q id='4haKL'></q></dir></style></legend>
                          <tbody id='4haKL'></tbody>

                          1. 主站蜘蛛池模板: 成人欧美一区二区三区在线播放 | 久草影视在线 | 亚洲毛片网站 | 天天影视亚洲综合网 | 麻豆91av | 久久久91精品国产一区二区精品 | 天天天操天天天干 | 国产精品国产馆在线真实露脸 | 一级欧美 | 毛片av免费在线观看 | 国产激情一区二区三区 | 亚洲永久在线 | 欧美一区二区大片 | 色播久久 | 一区二区三区视频在线观看 | 亚洲人成一区二区三区性色 | 天天看夜夜 | 欧美高清dvd | 日韩有码一区 | 国产成人免费视频网站视频社区 | 午夜免费 | 成人1区2区| 亚洲精品日韩在线 | 91久久视频 | 精品免费国产一区二区三区四区介绍 | 粉嫩一区二区三区四区公司1 | 亚洲一区二区在线播放 | 日韩精品在线观看免费 | 最新国产精品精品视频 | 中文字幕在线精品 | 99久久精品国产一区二区三区 | 九九成人 | 少妇一级淫片免费放播放 | 久久国产精品99久久久久久丝袜 | 日操夜操| 国产精品久久久久久久久久久久午夜片 | 国产精品久久久久久久免费大片 | 91精品一区| 中文字幕精品一区二区三区在线 | 亚洲精品91 | 福利视频网 |