Results 1 to 4 of 4
Hybrid View
-
23rd June 2010 17:24 #1
Èçáÿãâàíå íà namespace êîíôëèêò â Python
Çäðàâåéòå!
Êîãàòî ïîëçâàì Python çà âêàðâàíå íà ôàéëîâå â PostgreSQL áàçà-äàííè ñå ïîëó÷àâà ñëåäíàòà ãðåøêà:
, êîÿòî ñëåä ðîâåíå â Èíòåðíåò, ñî÷è êúì êîíôëèêò ìåæäó os.open() è âãðàäåíàòà open() ôóíêöèè. Ïðîáâàõ äà âúâåäà io.open() âìåñòî open(), íî ãðåøêàòà ñè îñòàâà.Code:Traceback <most recent call last>: File "insert_into_db_v9.py", line 54, in <module> TypeError: an integer is required
Èìàòå ëè íÿêàêâè èäåè êàêâî òðÿáâà äà ñå ïðîìåíè, çà äà "òðúãíå" êîäúò?
Êîäúò, êîéòî ñúì íàïèñúë å ñëåäíèÿò:
Code:import psycopg2 import os from os import sep, listdir, path import io libfolder = "C:\\Blender_Library\\BlenderLib\\objectLib\\" #we are in C:\\Blender_Library\\BlenderLib\\objectLib\\ for dir1 in os.listdir(libfolder): print libfolder + dir1 if os.path.isdir(os.path.dirname(libfolder + dir1)): tempdir = libfolder + dir1 tempfiles = os.listdir(tempdir) #we are for example in C:\\Blender_Library\\BlenderLib\\objectLib\\Osaka" for f in tempfiles: if os.path.isdir(libfolder + dir1 + os.sep + f): tempdir1 = libfolder + dir1 + os.sep + f tempfiles1 = os.listdir(tempdir1) #we are for example in C:\\Blender_Library\\BlenderLib\\objectLib\\Faqns\\Osaka" for ff in tempfiles1: print libfolder + dir1 + os.sep + f + os.sep + ff if ff[-5:] == "blend": # !!! that is test data. It must be changed conn=psycopg2.connect("host=localhost dbname=postgres user=postgres password=test") #conn.cursor will return a cursor oject, you can use this cursor to perform queries cursor = conn.cursor() # psycopg2.Binary() escapes all data that needs that data1 = psycopg2.Binary(io.open( tempdir1 + os.sep + ff, 'rb' ).read()) # execute our Query cursor.execute("""UPDATE testtable SET blend = %s) WHERE testtable_n = %s""", data1, str(os.path.splitext(ff)[0])) # Save (commit) the changes conn.commit() # We can also close the cursor if we are done with it cursor.close() elif ff[-3:] == "jpg" or ff[-4:] == "jpeg": # !!! that is test data. It must be changed conn=psycopg2.connect("host=localhost dbname=postgres user=postgres password=test") #conn.cursor will return a cursor oject, you can use this cursor to perform queries cursor = conn.cursor() # psycopg2.Binary() escapes all data that needs that file = io.open( tempdir1 + os.sep + ff, 'rb' ) data1 = file.read() # execute our Query cursor.execute("""UPDATE testtable SET jpeg = %s WHERE bibgood_n = %s""", psycopg2.Binary(data1), os.path.splitext(ff)[0]) # Save (commit) the changes conn.commit() # We can also close the cursor if we are done with it cursor.close() elif ff[-3:] == "txt": # !!! that is test data. It must be changed conn=psycopg2.connect("host=localhost dbname=postgres user=postgres password=test") #conn.cursor will return a cursor oject, you can use this cursor to perform queries cursor = conn.cursor() # psycopg2.Binary() escapes all data that needs that data1 = psycopg2.Binary(io.open( tempdir1 + os.sep + ff, 'r+b' ).read()) # execute our Query cursor.execute("""UPDATE testtable SET txt = %s) WHERE testtable_n = %s""", data1, str(os.path.splitext(ff)[0])) # Save (commit) the changes conn.commit() # We can also close the cursor if we are done with it cursor.close()
-
26th June 2010 12:19 #2
Íå ñúì ìíîãî íàÿñíî ñ Python âñå îùå, ïîðàäè êîåòî ñå èçâèíÿâàì çà íàìåñàòà, íî íàëè èìà âúçìîæíîñò äà ñúçäàâàø alias-è â Python íà ìîäóëè è òåõíèòå àòðèáóòè? Ïðèìåðíî,
èëèCode:import <blablabla> as <bla>
Òàêà íÿìà ëè äà ñòàíå?Code:from <foobar> import <bar> as <fugazi>
UD3R|Q9550+1283|4x2 G.Skill@1066|MX500+1002FAEX+640AAKS+ST31 60|RX580-8GB|1841BLT|SS-760XP|Define7|z24i|Edifier S350DB
geniusloci: ÀÌÄ ñå êóïóâà à) àêî ñè áåäåí; á) àêî ñà òå èçëúãàëè; â) àêî ñè áîëåí ôåí?
nope|r.i.p.
-
27th June 2010 14:57 #3
Àêî òîâà, êîåòî ñè ïåéñòíàë, å òî÷íîòî ñúäúðæàíèå íà ôàéëà, òî ðåä 54, êúäåòî ñòàâà ãðåøêàòà å:
Íà äðóãèòå äâå ìåñòà, êúäåòî èìàø àíàëîãè÷íè èçâèêâàíèÿ, èçïîëçâàø åäèí äîïúëíèòåëåí str.Code:WHERE bibgood_n = %s""", psycopg2.Binary(data1), os.path.splitext(ff)[0])
Internet - it doesn't make you stupid, it just makes your stupidity more accessible to others
-
28th June 2010 07:14 #4
System.gc(): ñ aliases íÿìà êàê äà ñòàíå, çàùîòî èíòåðïðåòàòîðà ãè ñâåæäà âèíàãè äî åäíî è ñúùî íåùî ïðè èçïúëíåíèåòî íà ñêðèïòà.
icaci: äà, òîâà å òî÷íîòî ìÿñòî íà ãðåøêàòà. Ïðîáâàõ è áåç str(), è ïàê èçêàðâà ãðåøêà.
Çà äà óëåñíÿ òåñòâàíåòî ïðîáâàõ ñëåäíèÿò êîä ñ psycopg2:
Ïîÿâÿâà ñå ñúùàòà ãðåøêà:Code:import psycopg2 import os import os.path import sys # !!! that is test data. It must be changed conn=psycopg2.connect("host='localhost' dbname='postgres' user='postgres' password='test'") #conn.cursor will return a cursor oject, you can use this cursor to perform queries cursor = conn.cursor() # psycopg2.Binary() escapes all data that needs that file = open( "C:\\Blender_Library\\BlenderLib\\objectLib\\Faqns\\Osaka2\\faqns_osaka_2.jpg", "rb" ) data1 = {'var':psycopg2.Binary(file.read())} # execute our Query cursor.execute("""UPDATE bibgood SET jpeg = %(var)s WHERE bibgood_n = %s""", data1, os.path.splitext('faqns_osaka_2.jpg')[0]) sys.stdout.flush() # Save (commit) the changes conn.commit() # We can also close the cursor if we are done with it cursor.close()
Ïðîáâàõ ñëåäíèÿò êîä è ñ pg8000:Code:Traceback <most recent call last>: File "insertdb_psycopg2.py", line 20, in <module> WHERE bibgood_n = %s""", data1, os.path.splitext('faqns_osaka_2.jpg')[0]) TypeError: an integer is required
Òóê ïúê ãðåøêàòà å:Code:from pg8000 import DBAPI import os import os.path import sys # !!! that is test data. It must be changed conn=DBAPI.connect(host="localhost", database="postgres", user="postgres", password="test") #conn.cursor will return a cursor oject, you can use this cursor to perform queries cursor = conn.cursor() file = open( "C:\\Blender_Library\\BlenderLib\\objectLib\\Faqns\\Osaka2\\faqns_osaka_2.jpg", "rb" ) data1 = DBAPI.Binary(file.read()) data2 = 'faqns_osaka_2' # execute our Query cursor.execute("UPDATE bibgood SET jpeg = %s WHERE bibgood_n = %s", data1, data2) sys.stdout.flush() # Save (commit) the changes conn.commit() # We can also close the cursor if we are done with it cursor.close()
Âå÷å îïðåäåëåíî ñúì çàãàçèë.Code:Traceback <most recent call last>: File "insertdb_pg8000.py", line 19, in <module> cursor.execute("UPDATE bibgood SET jpeg = %s WHERE bibgood_n = %s", data1, data2) File "build\bdist.win32\egg\pg8000\dbapi.py", line 243, in _fn TypeError: execute() takes at most 3 arguments (4 given)
---------- Äîáàâåí ïîñëåäâàù êîìåíòàð â 07:14 ---------- Îðèãèíàëíèÿò êîìåíòàð å ïóñíàò â÷åðà â 23:50 ----------
Ãðåøêàòà áåøå îòêðèòà. Ïðè psycopg2 ðåøåíèåòî áåøå äà ïðîìåíÿ ñëåäíèÿò ðåä:
íàCode:WHERE bibgood_n = %s""", data1, os.path.splitext('faqns_osaka_2.jpg')[0])
Àêî íå âèæäàòå ðàçëèêàòà ìåæäó äâàòà ðåäà â íà÷àëîòî, íå ñå ó÷óäâàéòå. Òÿ ñå ñúñòîè â äîáàâÿíåòî íà '(' ïðåäè data1, è ')' ñëåä 'faqns_osaka_2.jpg')[0]. Î÷åâèäíî ÐÅÐ249 å áèë èíòåðïðåòèðàí ìíîãî "èíòåðåñíî".Code:WHERE bibgood_n = %s""", (data1, os.path.splitext('faqns_osaka_2.jpg')[0]))
Áåøå äúëãà íîù!Last edited by haste; 28th June 2010 at 01:14. Reason: êîðåêöèÿ íà êîäà




Reply With Quote
Lenovo ThinkPad 15 èëè IdeaPad 15
5th May 2023, 22:16 in Ìîáèëíè êîìïþòðè