Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 21st, 2006, 1:49 AM   #21
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
Here is something I worked up to show you how to input data in MySQL via the command line in Python... should be easier to follow than the other example.

mysql> desc users;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| userid | varchar(10) | YES  |     | NULL    |       |
| fname  | varchar(20) | YES  |     | NULL    |       |
| lname  | varchar(25) | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+


mysql> select * from users;
Empty set (0.00 sec)


Executed below script...
#python db.py test test test


mysql> select * from users;
+--------+-------+-------+
| userid | fname | lname |
+--------+-------+-------+
| test   | test  | test  |
+--------+-------+-------+
1 row in set (0.00 sec)



#! /usr/bin/python

import sys
import MySQLdb

id = sys.argv[1]
fname = sys.argv[2]
lname = sys.argv[3]

class Table:
     def __init__(self, db, name):
            self.db = db
            self.name = name
            self.dbc = self.db.cursor()

     def additem(self, item):
         sql = "INSERT INTO " + self.name + " VALUES ('" + id + "','" + fname + "','" + lname +"')"
         self.dbc.execute(sql)
         return

def main():
    db = MySQLdb.connect(db="DBNAME",host="HOSTNAME",user="USERNAME",passwd="PASSWORD");
    table = Table(db, "TABLENAME")
    table.additem("TABLENAME")

if __name__ == '__main__':
     main()


Let me know if you have trouble working in your specific tables... should be a matter of substitution of table name, fields, etc.
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."

Last edited by Infinite Recursion; Oct 21st, 2006 at 2:44 AM.
Infinite Recursion is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:17 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC