Python EXECUTEMANY doesn't work

141 views
Skip to first unread message

Kyle Felipe Vieira Roberto

unread,
Feb 20, 2018, 8:55:47 AM2/20/18
to SpatiaLite Users
Hi guys!
I was helping @Yjmenezes with a python code and i had a problem with the folow code:

distancia = []
for i in range(145, 13000, 145):
     distancia
.append((float(i)/100))
sql
= """INSERT INTO linhas_paralelas(geom)
         SELECT ST_OffsetCurve(geometry,?) from paracatu;"""

con
.executesmany(sql, distancia)
con
.commit()
con
.close()


So, when use the executemany, the lines are not created, but, if use the follow code, works well.

distancia = []
for i in range(145, 13000, 145):
     distancia
.append((float(i) / 100))
sql2
= ""
for i in distancia:
 sql2
+= """INSERT INTO linhas_paralelas(geom)
SELECT ST_OffsetCurve(geometry,{dist}) from paracatu;\n"""
.format(dist=i)
con
.executescript(sql2)
con
.commit()
con
.close()

There is a way to use the executemany??
Im using:
Python 2.7
package: sqlite3
SQLITE VERSION: 3.16.2
SPATIALITE VERSION: 4.3.0a 


a.fu...@lqt.it

unread,
Feb 23, 2018, 7:25:37 AM2/23/18
to spatiali...@googlegroups.com
On Tue, 20 Feb 2018 05:55:47 -0800 (PST), Kyle Felipe Vieira Roberto
wrote:
> Hi guys!
> I was helping @Yjmenezes with a python code and i had a problem with
> the folow code:
>
> distancia = []
> for i in range(145, 13000, 145):
>      distancia.append((float(i)/100))
> sql = """INSERT INTO linhas_paralelas(geom)
>          SELECT ST_OffsetCurve(geometry,?) from paracatu;"""
> con.executesmany(sql, distancia)
> con.commit()
> con.close()
>

Hi Kyle,

I'm not a Python programmer, so I've just tested a very
simple script.
it seems that "executemany" always expects that at least
_TWO_ bindings must be specified for each row.

test #1
=========
distancia = [1.1, 2.2, 3.3)
sql = """INSERT INTO linhas_paralelas(geom)
SELECT ST_OffsetCurve(geometry,?) from paracatu;"""
con.executesmany(sql, distancia)

this fails, returning this error message:
"Incorrect number of bindigs supplied. The current statement
uses 1, and there are 3 supplied."


test #2
=========
distancia = [(1.1), (2.2), (3.3))
sql = """INSERT INTO linhas_paralelas(geom)
SELECT ST_OffsetCurve(geometry,?) from paracatu;"""
con.executesmany(sql, distancia)

same errore as above.


test #3
=========
distancia = [(None, 1.1), (None, 2.2), (None, 3.3))
sql = """INSERT INTO linhas_paralelas(id, geom)
SELECT ?, ST_OffsetCurve(geometry,?) from paracatu;"""
con.executesmany(sql, distancia)

and finally this last test nicely works; but as you
can easily notice wa are now using 2 bindings for
each row.
I strongly suspect that this odd behavior is someway
related to Python arrays, but my personal skills about
this language are too limited to say any more.

bye Sandro


Kyle Felipe Vieira Roberto

unread,
Feb 23, 2018, 1:54:29 PM2/23/18
to SpatiaLite Users
@Sandro.
I did not pay atention  to python docs, there thy show the right way usage..
so your last code example is correct.

Kyle Felipe Vieira Roberto

unread,
Feb 23, 2018, 3:02:02 PM2/23/18
to SpatiaLite Users
Reply all
Reply to author
Forward
0 new messages