Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [{"1":"1","2":"1"},...]
- import pandas as pd
- from sqlalchemy import create_engine
- data = [{'a':1, 'b':'string1'}, {'a':2, 'b':'string2'}, {'a':3, 'b':'string3'}]
- # create SQL Alchemy DB connection
- # conn = create_engine('postgresql://user:password@host:port/dbname')
- conn = create_engine('postgresql+psycopg2://user:password@host:port/dbname')
- # create Pandas DataFrame from the list of records
- df = pd.DataFrame(data)
- # write DF into SQL table
- df.to_sql('table_name', conn, if_exists='replace', index=False)
- In [74]: df
- Out[74]:
- a b
- 0 1 string1
- 1 2 string2
- 2 3 string3
- import psycopg2
- out = [{'a':1,'b':2,'c':3}, {'a':4,'b':5,'c':6}]
- with psycopg2.connect("dbname='dbname' user='user' host='host' password='password'") as conn1:
- with conn1.cursor() as cur:
- cur.execute("CREATE TABLE table (a varchar, b varchar, c varchar);")
- cur.executemany("INSERT INTO table VALUES (%(a)s, %(b)s, %(c)s);", out)
- conn1.commit()
Add Comment
Please, Sign In to add comment