View Single Post
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