Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita64558e

Browse files
committed
Add simple database and sqlite3 demonstration
1 parentbd28590 commita64558e

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

‎DatabaseDemo/.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>DatabaseDemo</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.python.pydev.PyDevBuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.python.pydev.pythonNature</nature>
16+
</natures>
17+
</projectDescription>

‎DatabaseDemo/.pydevproject

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<?eclipse-pydev version="1.0"?><pydev_project>
3+
<pydev_pathpropertyname="org.python.pydev.PROJECT_SOURCE_PATH">
4+
<path>/${PROJECT_DIR_NAME}/src</path>
5+
</pydev_pathproperty>
6+
<pydev_propertyname="org.python.pydev.PYTHON_PROJECT_VERSION">python 3.6</pydev_property>
7+
<pydev_propertyname="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
8+
</pydev_project>

‎DatabaseDemo/src/databasedemo.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
'''
2+
Created on Aug 8, 2017
3+
4+
@author: Aditya
5+
6+
7+
This program gives demo for Database useage in python
8+
'''
9+
importsqlite3# import python library that supports sqlite3
10+
11+
defcreate_database(filename):
12+
db=sqlite3.connect(filename)# connects the file and creates the database if it did not exist
13+
returndb
14+
15+
defreadwrite_database():
16+
filename='demo.db'# database filename
17+
db=create_database(filename)
18+
db.execute('drop table if exists testtable')# to remove table of name 'testtable' if existing
19+
db.execute('create table testtable (t1 text, i1 int)')# create table 'testtable' with columns (t1,i1)=(text,int)
20+
db.execute('insert into testtable (t1, i1) values (?, ?)', ('one',1))# insert values in columns (t1,i1) of database testtable
21+
db.execute('insert into testtable (t1, i1) values (?, ?)', ('two',2))
22+
db.execute('insert into testtable (t1, i1) values (?, ?)', ('three',3))
23+
db.execute('insert into testtable (t1, i1) values (?, ?)', ('four',4))
24+
db.execute('insert into testtable (t1, i1) values (?, ?)', ('five',5))
25+
db.execute('insert into testtable (t1, i1) values (?, ?)', ('six',6))
26+
db.commit()# commit after changing any data in database
27+
28+
cursor=db.execute('select * from testtable order by t1')# select data from testtable in order of t1 column
29+
forrowincursor:
30+
print(row)
31+
32+
cursor=db.execute('select i1, t1 from testtable order by i1')# select data from testtable in order of i1 column and also change the order of display
33+
forrowincursor:
34+
print(row)
35+
36+
defusing_rowfactory():
37+
filename='demo.db'# database filename
38+
db=create_database(filename)
39+
db.row_factory=sqlite3.Row
40+
db.execute('drop table if exists testtable')# to remove table of name 'testtable' if existing
41+
db.execute('create table testtable (t1 text, i1 int)')# create table 'testtable' with columns (t1,i1)=(text,int)
42+
db.execute('insert into testtable (t1, i1) values (?, ?)', ('one',1))# insert values in columns (t1,i1) of database testtable
43+
db.execute('insert into testtable (t1, i1) values (?, ?)', ('two',2))
44+
db.execute('insert into testtable (t1, i1) values (?, ?)', ('three',3))
45+
db.execute('insert into testtable (t1, i1) values (?, ?)', ('four',4))
46+
db.execute('insert into testtable (t1, i1) values (?, ?)', ('five',5))
47+
db.execute('insert into testtable (t1, i1) values (?, ?)', ('six',6))
48+
db.commit()# commit after changing any data in database
49+
50+
cursor=db.execute('select * from testtable order by t1')# select data from testtable in order of t1 column
51+
forrowincursor:
52+
print(row)
53+
print('Notice: the iterator returns row objects. Read these row objects as dictionaries.')
54+
55+
cursor=db.execute('select * from testtable order by t1')# select data from testtable in order of t1 column
56+
forrowincursor:
57+
print(dict(row))
58+
59+
cursor=db.execute('select * from testtable order by t1')# select data from testtable in order of t1 column
60+
forrowincursor:
61+
print(row['t1'])
62+
print(row['t1'],row['i1'])
63+
64+
65+
defmain():
66+
readwrite_database()
67+
using_rowfactory()
68+
69+
if__name__=='__main__':main()

‎DatabaseDemo/src/demo.db

8 KB
Binary file not shown.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp