Create MySQLdb database using Python script -


i'm having troubles creating database , tables. database needs created within python script.

#connect method has 4 parameters: #localhost (where mysql db located),  #database user name,  #account password,  #database name     db1 = ms.connect(host="localhost",user="root",passwd="****",db="test") 

returns

_mysql_exceptions.operationalerror: (1049, "unknown database 'test'") 

so clearly, db1 needs created first, how? i've tried create before connect() statement errors.

once database created, how create tables? thanks, tom

here syntax, works, @ least first time around. second time naturally returns db exists. figure out how use drop command properly.

   db = ms.connect(host="localhost",user="root",passwd="****")    db1 = db.cursor()    db1.execute('create database test1') 

so works great first time through. second time through provides warning "db exists". how deal this? following how think should work, doesn't. or should if statement, looking if exists, not populate?

import warnings warnings.filterwarnings("ignore", "test1") 

use create database create database:

db1 = ms.connect(host="localhost",user="root",passwd="****") cursor = db1.cursor() sql = 'create database mydata' cursor.execute(sql) 

use create table create table:

sql = '''create table foo (        bar varchar(50) default null        ) engine=myisam default charset=latin1        ''' cursor.execute(sql) 

there lot of options when creating table. if not sure right sql should be, may use graphical tool phpmyadmin create table, , use show create table discover sql needed create it:

mysql> show create table foo \g *************************** 1. row ***************************        table: foo create table: create table `foo` (   `bar` varchar(50) default null ) engine=myisam default charset=latin1 1 row in set (0.00 sec) 

phpmyadmin can show sql used perform sorts of operations. can convenient way learn basic sql.

once you've experimented this, can write sql in python.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -