[Tutor] I've been trying to make this program work so i can transfer it to final user. please do have a look and see if the code can be corrected in anyway.

Alan Gauld alan.gauld at btinternet.com
Mon Sep 29 14:08:46 CEST 2014


On 26/09/14 12:58, Brave Heart wrote:

>    File "C:/Python34/cash.py", line 339, in add_func
>      os.chdir(sqlite3_path)
> FileNotFoundError: [WinError 2] The system cannot find the file specified: 'C:/sqlite3/'

I assume you have checked that the folder exists? and
that you (ie. the user running the program) have permission
to go there?


> def add_func(dt, ds, at):
>      '''Insert data into db(L2)'''
>      sql_insert = """
>      INSERT INTO expenses (date, desc, amount)
>      VALUES ('%s', '%s', '%s')
>      """ % (dt, ds, at)
>
>      os.chdir(sqlite3_path)
>
>      # open connection to database
>      try:
>          cx = sqlite.connect(database)
>      except sqlite.Error(errmsg):
>          print ("Can not open ") +str(errmsg)
>
>      # insert data into table
>      try:
>          cu = cx.cursor()
>          cu.execute(sql_insert)
>          cx.commit()
>      except sqlite.Error(errmsg):
>          print ("Can not execute: ") +str(errmsg)
>
>      # close connection
>      cx.close()

BTW Its probably better to set the database to the full path rather than 
change into the folder for every access of the database.
In fact I usually just open the database once at the start
of my program. The data functions then only need to create
a cursor (in fact I often keep the cursor open too!).

That reduces the work in each function and the duplication of code
although you could put the open and cursor code into a function
called by the other data functions - that would help with
maintenance in future.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list