4 Replies - 6106 Views - Last Post: 16 November 2010 - 03:07 PM Rate Topic: -----

#1 Gamegoofs2  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 159
  • Joined: 15-April 09

Saving Data with Python

Posted 15 November 2010 - 02:15 PM

I want to save url addresses using python and then retrieve them later. I'm use to using PHP and just inputing something like this into a database. My first thoughts were to create an XML or JSON file and store the urls there, but is there a better way to save data?
Is This A Good Question/Topic? 0
  • +

Replies To: Saving Data with Python

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4444
  • View blog
  • Posts: 12,318
  • Joined: 18-April 07

Re: Saving Data with Python

Posted 15 November 2010 - 03:07 PM

You can save data in a varying number of ways with Python, just like you can in PHP or C# or Perl etc. Python can use databases just like PHP can... here is a link to an about.com page that contains links to various database articles for Python.

Programming Python - Python Database Programming - About.com

You can save to file also, like you said, as well as mimicing session/cookie data etc. It is all possible with Python and depends on what you really need to save and how you want to interact with that data. Typically a database is a pretty easy to access permanent secure form (if done properly) but if you need something quick and doesn't need to be secure, a file could work also. All depends on what you are trying to do.

Enjoy! :)
Was This Post Helpful? 0
  • +
  • -

#3 Gamegoofs2  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 159
  • Joined: 15-April 09

Re: Saving Data with Python

Posted 15 November 2010 - 05:37 PM

I'm making a desktop application. So creating a file is best?
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4444
  • View blog
  • Posts: 12,318
  • Joined: 18-April 07

Re: Saving Data with Python

Posted 15 November 2010 - 10:57 PM

Well just because it is a desktop app doesn't mean by default it is a file approach for saving. It really depends on what the data is used for and how it may be accessed. Will the data need to be shared across a network? Will other users need to see this data? Does it need to be secure? It just depends. If you need a quick and dirty way to save data for the app to use and it is not crucial data, a file could work. If it needs to be accessed by others and more secure, a database may be the right choice.

Pick whichever method seems to best fit your data access needs and security concerns. :)
Was This Post Helpful? 0
  • +
  • -

#5 kiwi_steve  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 109
  • Joined: 26-September 09

Re: Saving Data with Python

Posted 16 November 2010 - 03:07 PM

I don't know if this will help or not, but I have just been teaching myself the Python SQLite module (which I have found rather simple to implement compared to other languages) to do something similar... heres the test code I cobbled together from various tutorials and my own tinkering - it should be relatively straight forward to figure out.

Cheers

Steve

#-------------------------------------------------------------------------------
# Name:        Database Test
# Purpose:
#
# Author:      Steve
#
# Created:     1/11/2010
# Copyright:   (c) Steve 2010
# Licence:     Beerware - If you like it, buy me beer :)/>
#-------------------------------------------------------------------------------
#!/usr/bin/env python

from sqlite3 import *
import sys
from random import randint

conn = connect("testdata.db")
cursor = conn.cursor()

try:
    cursor.execute('CREATE TABLE customers (id INTEGER PRIMARY KEY, fname VARCHAR(50), lname VARCHAR(50), email VARCHAR(50))')
    print "Table 'customers' created successfully"
except OperationalError:
    print"This table already exists..."

try:
    print"Writing customer details now"
    for i in range (10):
        print i,
        x = randint(1,20000)
        try:
            cursor.execute('INSERT INTO customers VALUES(?, "John", "Smith", "jsmith@jsmith.com")',(x,))
            if x%5 == 0:
                print "\n", x,"is a multiple of 5, deleting this value"
                conn.execute('DELETE FROM customers WHERE id=?', (x,))
        except IntegrityError:
            print "\ndouble-up:",x
    print"\n\nDone..."
except OperationalError:
    print"Database write error - is the database set to read only?"

conn.commit() # commit the changes to the database (ie: write them)

cursor.execute('SELECT * FROM customers')
for row in cursor:
    print 'ID', row[0]
    print 'Name', row[1], row[2]
    print 'Email', row[3]
    print '-'*10

cursor.close() # close the cursor
conn.close()   # then close the database


This post has been edited by kiwi_steve: 16 November 2010 - 03:09 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1