Klug pgsql tut

594 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
594
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Klug pgsql tut

  1. 1. selvamani@selvamani-laptop:~$ sudo su - postgres [sudo] password for selvamani: postgres@selvamani-laptop:~$ psql -U pgsqldemouser pgsqldemodb psql (8.4.3) Type "help" for help. pgsqldemodb=> create table pgsqldemotable(sno varchar(3), name varchar(30)); CREATE TABLE pgsqldemodb=> insert into pgsqldemotable values('1','Arulalan'); INSERT 0 1 pgsqldemodb=> insert into pgsqldemotable values('2','BalaKrishnan'); INSERT 0 1 pgsqldemodb=> insert into pgsqldemotable values('3','Dhasthagheer'); INSERT 0 1 pgsqldemodb=> insert into pgsqldemotable values('4','Selvamani'); INSERT 0 1 pgsqldemodb=> select * from pgsqldemotable; sno | name -----+-------------- 1 | Arulalan 2 | BalaKrishnan 3 | Dhasthagheer 4 | Selvamani (4 rows) pgsqldemodb=> alter role pgsqldemouser with password 'demo'; ALTER ROLE pgsqldemodb=> q postgres@selvamani-laptop:~$ logout selvamani@selvamani-laptop:~$ python Python 2.6.4 (r264:75706, Dec 7 2009, 18:45:15) [GCC 4.4.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import psycopg2 >>> connection=psycopg2.connect("host=localhost dbname=pgsqldemodb user=pgsqldemouser password=demo") cur=connection.cursor() >>> cur.execute("""select * from pgsqldemotable""") >>> a=cur.fetchall() >>> print a [('1', 'Arulalan'), ('2', 'BalaKrishnan'),('3','Dhasthagheer'), ('4', 'Selvamani')] >>> type(a) <type 'list'> >>> type(a[0]) <type 'tuple'> >>> type(a[0][0]) <type 'str'> >>> type(a[0][1]) <type 'str'> >>> a[0][1] 'Arulalan' >>> a[1][1]
  2. 2. 'BalaKrishnan' >>> cur.execute("""insert into pgsqldemotable values('5','Rajkumar')""") >>> connection.commit() >>> sno=raw_input("""Enter the value:""") Enter the value:6 >>> name=raw_input("""Enter the value:""") Enter the value:Shrinivasan >>> cur.execute("""insert into pgsqldemotable values('%s','%s')""" %(sno,name)) >>> connection.commit() >>> exit() selvamani@selvamani-laptop:~$ sudo su - postgres postgres@selvamani-laptop:~$ psql -U pgsqldemouser -W pgsqldemodb Password for user pgsqldemouser: psql (8.4.3) Type "help" for help. pgsqldemodb=> select * from pgsqldemotable; sno | name -----+-------------- 1 | Arulalan 2 | BalaKrishnan 3 | Dhasthagheer 4 | Selvamani 5 | Rajkumar 6 | Shrinivasan (6 rows) pgsqldemodb=> q postgres@selvamani-laptop:~$ logout selvamani@selvamani-laptop:~$

×