1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import psycopg2 # Open a DB session dbSession = psycopg2.connect("dbname='socialmedia' user='postgres' password='abc'"); dbSession.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT); # Open a database cursor dbCursor = dbSession.cursor(); # SQL statement to create a table sqlCreateTable = "CREATE TABLE test_table(id bigint, cityname varchar(128), latitude numeric, longitude numeric);"; # Execute CREATE TABLE command dbCursor.execute(sqlCreateTable); # Insert statements sqlInsertRow1 = "INSERT INTO test_table values(1, 'New York City', 40.73, -73.93)"; sqlInsertRow2 = "INSERT INTO test_table values(2, 'San Francisco', 37.733, -122.446)"; # Insert statement dbCursor.execute(sqlInsertRow1); dbCursor.execute(sqlInsertRow2); # Select statement sqlSelect = "select * from test_table"; dbCursor.execute(sqlSelect); rows = dbCursor.fetchall(); # Print rows for row in rows: print(row); |
結果
(1, ‘New York City’, Decimal(‘40.73’), Decimal(‘-73.93’))
(2, ‘San Francisco’, Decimal(‘37.733’), Decimal(‘-122.446’))
コメントを残す