Results 1 to 4 of 4

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Banned haste's Avatar
    Join Date: Mar:2006
    Location:
    Posts: 759

    namespace Python

    !

    Python PostgreSQL - :

    Code:
    Traceback <most recent call last>:
      File "insert_into_db_v9.py", line 54, in <module>
    TypeError: an integer is required
    , , os.open() open() . io.open() open(), .

    , "" ?

    , :

    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()

  2. #2
    System.gc()'s Avatar
    Join Date: Apr:2007
    Location: back in time
    Posts: 1,075
    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.

  3. #3
    philosophus duratea icaci's Avatar
    Join Date: Oct:2006
    Location: Aachen
    Posts: 2,698
    , , , 54, :
    Code:
    WHERE bibgood_n = %s""", psycopg2.Binary(data1), os.path.splitext(ff)[0])
    , , str.
    Internet - it doesn't make you stupid, it just makes your stupidity more accessible to others

  4. #4
    Banned haste's Avatar
    Join Date: Mar:2006
    Location:
    Posts: 759
    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()
    :
    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
    pg8000:

    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])


    Code:
    WHERE bibgood_n = %s""", (data1, os.path.splitext('faqns_osaka_2.jpg')[0]))
    , . '(' data1, ')' 'faqns_osaka_2.jpg')[0]. 249 "".

    !
    Last edited by haste; 28th June 2010 at 01:14. Reason:

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Copyright © 1999-2011 . .
iskamPC.com | mobility.BG | Bloody's Techblog | | 3D Vision Blog |