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?

B.K.
  • 9,982
  • 10
  • 73
  • 105
xyz
  • 1
  • 1

1 Answers1

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()
fnatic_shank
  • 572
  • 6
  • 12
  • 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 Jul 24 '14 at 05:26
  • @xyz .. You can see the exact date/time stamps supported by postresql [here](http://www.postgresql.org/docs/current/static/datatype-datetime.html) and for more psycopg2 usage refer [here](http://initd.org/psycopg/docs/usage.html#date-time-objects-adaptation). I think it will get you started. – fnatic_shank Jul 24 '14 at 07:25
  • 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. – xyz Jul 24 '14 at 14:03
  • This [site](http://www.python-excel.org/) should be useful. But I recommend you work with .csv files. It is much easier to manipulate data in csv using python ([read here](https://docs.python.org/2/library/csv.html)). You can always save your xlsx files as csv and vice-versa. Read [this](http://stackoverflow.com/questions/3207219/how-to-list-all-files-of-a-directory-in-python) on how to access files of a folders in python. – fnatic_shank Jul 25 '14 at 21:33