I like to create a database of common chemicals with Python. How would I go about that? Any help welcome!

Recommended Answers

All 7 Replies

Python is a programming language, is that what you propose to the the front end of the application? if using python, use MySQL database to create your database to save the date and python to retrieve and manipulate it.

Rather then MySQL you can use SQLite, sqlite3 is built into Python version 2.5 or you can download pysqlite2 for older versions.

Here is an example to create a database file:

# create a SQL database file
# pySQLite homepage (for Python24): http://initd.org/tracker/pysqlite
# tested with Python25 (has sqlite3 builtin)

try:
    from sqlite3 import dbapi2 as sqlite    # Python25
except ImportError:
    from pysqlite2 import dbapi2 as sqlite  # Python24 and pysqlite

# create a database:
con = sqlite.connect('mydatabase.db3')
cur = con.cursor()

# create a table:
cur.execute('create table clients (id INT PRIMARY KEY, name CHAR(60))')

# insert a single line:
client = (5,"John Smith")
cur.execute("insert into clients (id, name) values (?, ?)", client )
con.commit()

# insert several lines at once:
clients = [ 
(7,"Ella Fitzgerald"),
(8,"Louis Armstrong"),
(9,"Miles Davis")
]
cur.executemany("insert into clients (id, name) values (?, ?)", clients )
con.commit()

cur.close()
con.close()

Here is the example that reads the database file:

# read a SQL database file

try:
    from sqlite3 import dbapi2 as sqlite    # Python25
except ImportError:
    from pysqlite2 import dbapi2 as sqlite  # Python24 and pySqlite

# connect to an existing database
con = sqlite.connect('mydatabase.db3')
cur = con.cursor()

# get row by row
print "Row by row:"
cur.execute('select id, name from clients order by name;')
row = cur.fetchone()
while row:
    print row
    row = cur.fetchone()

# get all rows at once:
print "All rows at once:"
cur.execute('select id, name from clients order by name;')
print cur.fetchall()

cur.close()
con.close()

If you don't want to get into the complexities of a standard sql database, you could simply modifiy my little structure/record based database in the Python code snippets at:
http://www.daniweb.com/code/snippet604.html

It allows you to load, save, add, list (as table), search, and delete data items. Need any help with that, just ask here.

great post

i've seen your other example of a database...but how would we do if we want to change for example the name of the movie without having to delete it and then add it again...

Might have to add an edit option to the search_title() function. Good idea though! I am thinking of wrapping this all into a Tkinter GUI, that would make editing easier using an Entry widget.

I've been trying to do that alteration that i talked above but i didin't get any results...can you give some advice how i would do that...thanks in adavance...:cheesy:

I've been trying to do that alteration that i talked above but i didin't get any results...can you give some advice how i would do that...thanks in adavance...:cheesy:

Take another look at:
http://www.daniweb.com/code/snippet604.html
I updated it with a few lines of code to add the edit feature. Thanks for bringing this to my attention.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.