0

I have several weather-related fields such as date_of_data_collection, time, temperature and relative humidity. I have to insert the values of these fields into PostgreSQL. Also, I want to use date and timestamp format for storing date_of_data_collection and time. The table has already been created. However, insertion is the main concern. How can my task be accomplished in Python?

2
  • Please the code you've tried Commented Jul 24, 2014 at 4:05
  • Start with the user manual for psycopg2 and the postgresql tutorial. Commented Jul 24, 2014 at 4:15

1 Answer 1

3

There are 2 ways to connect to a database from python:

  1. An ORM like SQLAlchemy
  2. A database driver/adapter like psycopg2

These are two completely different things:

  • SQLAlchemy generates SQL statements, and
  • psycopg2 directly sends SQL statements to the database.

Note: SQLAlchemy depends on psycopg2 or other database drivers to communicate with the database.

Using an ORM will boost your development speed, and you can generate SQL statements by writing in a pythonic way. For example,

from sqlalchemy import create_engine
dbengine = create_engine('postgresql://user@localhost:5432/db')

# Lets create a table with 2 columns
users = Table('users', metadata,
          Column('name', String(40), primary_key=True)
          Column('age', Integer)
        )
users.create()

# And insert some data
u = users.insert()
u.execute (
          {'name': 'John', 'age': 24},
          {'date': 'Doe', 'age': 42},
)

Tip: I prefer using an ORM and SQLAlchemy is excellent library for that.

(Also, it saves you from writing sql)

Using a database driver- psycopg2 you can read more about it here

import psycopg2
DATABASE = psycopg2.connect("dbname='database_name' user='dbuser' host='localhost' password='dbpass'")

# Next step is to define a cursor to work with.
db = DATABASE.cursor()
# Now we can write our SQL statements
db.execute("""SELECT * from table_name""")

# create a table
db.execute('''CREATE TABLE users
          (NAME    TEXT    NOT NULL,
           AGE     INT     NOT NULL,
          );'''
)

# And insert some data
db.execute("INSERT INTO users (NAME,AGE) VALUES ('John', 24)");
db.execute("INSERT INTO users (NAME,AGE) VALUES ('Doe', 42)");

# close communication with database
db.commit()
db.close()
Sign up to request clarification or add additional context in comments.

4 Comments

thank you very much for your immediate response but can you just suggest me the insert query for inserting multiple records of weather fields that i've mentioned in my query with date in date format and time in timestamp format, and I want it usin pyscopg2 module.
@xyz .. You can see the exact date/time stamps supported by postresql here and for more psycopg2 usage refer here. I think it will get you started.
thanks the links that you have provided were of grt help. And do you have any idea about how to read a complete folder containing xlsx files using python again.
This site should be useful. But I recommend you work with .csv files. It is much easier to manipulate data in csv using python (read here). You can always save your xlsx files as csv and vice-versa. Read this on how to access files of a folders in python.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.