On Oct 23, 1:00 pm, "Rick Harding" <deuce...@gmail.com> wrote:
Snip...
> db_egine.text = add_user_query
> result = db_engine.execute({'username': username,
> 'password': password,
> 'uid': CONFIG['UID'],
> 'gid': CONFIG['GID'],
> 'home': '/home/' + username + '/www/'})
>
Rick,
I'm not a SQLAchemy user but is the
problem as simple as a typo?
Should
"db_egine.text = add_user_query"
be
"db_engine.text = add_user_query" ?
- Jim McDonald
No problem.
If you're open to something other than
SQLAlchemy and your posted code maps
almost exactly to the Python DB-API.
To use just the straight Python DB-API
I'd try something like the following.
(Note: I didn't actually try to run the code
since I don't have the proper tables or your
config files, etc.)
- Jim C.
# ###############################
import MySQLdb
# create a connection
mydb = MySQLdb.Connect( db=CONFIG["DBDATABASE"],
user=CONFIG["DBDATABASE"],
passwd=CONFIG["DBPASSWORD"]
)
# create a cursor
cursor = mydb.cursor()
# fixed SQL text
add_user_query = """
INSERT INTO `ftpuser` (
`id` ,
`userid` ,
`passwd` ,
`uid` ,
`gid` ,
`homedir` ,
`shell` , `count` ,
`accessed` ,
`modified`
)
VALUES (
NULL ,
%(username)s,
ENCRYPT( %(password)s ),
%(uid)s,
%(gid)s,
%(home)s,
'/sbin/nologin', '0', '', NOW( )
);
"""
# put parameters in a dictionary
parameter_dict = { 'username': username,
'password': password,
'uid': CONFIG['UID'],
'gid': CONFIG['GID'],
'home': '/home/' + username + '/www/'
}
# execute the SQL using parms
cursor.execute(add_user_query,parameter_dict)
# fetch a result set
result_set = cursor.fetchall()
# call a commit
mydb.commit()
# #############################
> Thanks for taking at look at it though.
>
No problem.
If you're open to something other than
SQLAlchemy and your posted code maps
almost exactly to the Python DB-API.
To use just the straight Python DB-API
I'd try something like the following.
(Note: I didn't actually try to run the code
since I don't have the proper tables or your
config files, etc.)
> Thanks for the help. I know that SqlAlchemy uses mysqldb and I got looking
> to make sure that mysqldb will properly escape things when used that way as
> if they are bound parameters. It looks like it does so I changed my code to
> instead just use %s in the query and then execute the query like so and it's
> working:
Rick,
That makes sense.
Now that you mention it, I believe that
MySQLdb only supports the "format" and
"pyformat" paramstyle (and not the "named"
paramstyle used in your first post) .
- Jim McDonald