Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > creating a new database with mysqldb

Reply
Thread Tools

creating a new database with mysqldb

 
 
John Salerno
Guest
Posts: n/a
 
      05-17-2006
Since the connect method of mysqldb requires a database name, it seems
like you can't use it without having a database already created. So is
there a way to connect to your mysql server (without a specified
database) in order to create a new database (i.e., the CREATE DATABASE
query)?

Thanks.
 
Reply With Quote
 
 
 
 
BartlebyScrivener
Guest
Posts: n/a
 
      05-17-2006
Type:

create a new database with mysql

into google and see what happens

rd

 
Reply With Quote
 
 
 
 
John Salerno
Guest
Posts: n/a
 
      05-17-2006
BartlebyScrivener wrote:
> Type:
>
> create a new database with mysql
>
> into google and see what happens
>
> rd
>


Well, the thing about it is that all the guides I find online seem to
begin with using a command prompt or a unix shell, neither of which will
work in my case. I'm trying to find a way to access my database server
using just a python script. Perhaps that isn't even possible for me to
do without shell access. I might just have to use the msqladministrator
in my server control panel, instead of using python.
 
Reply With Quote
 
Philippe Martin
Guest
Posts: n/a
 
      05-17-2006
John Salerno wrote:

> Since the connect method of mysqldb requires a database name, it seems
> like you can't use it without having a database already created. So is
> there a way to connect to your mysql server (without a specified
> database) in order to create a new database (i.e., the CREATE DATABASE
> query)?
>
> Thanks.


I'm no expert but: can't you spawn mysql with a script/scheme ?

Philippe

 
Reply With Quote
 
Heiko Wundram
Guest
Posts: n/a
 
      05-17-2006
Am Mittwoch 17 Mai 2006 21:23 schrieb John Salerno:
> Well, the thing about it is that all the guides I find online seem to
> begin with using a command prompt or a unix shell, neither of which will
> work in my case. I'm trying to find a way to access my database server
> using just a python script. Perhaps that isn't even possible for me to
> do without shell access. I might just have to use the msqladministrator
> in my server control panel, instead of using python.


Creating a database is just another SQL command in MySQL (which you can easily
send to the MySQL server you're using with Python and MySQLdb):

"CREATE DATABASE <dbname>"

Of course, you need to log on with a user who is allowed to create databases.

See the MySQL documentation for more info on the available CREATE commands.

--- Heiko.
 
Reply With Quote
 
BartlebyScrivener
Guest
Posts: n/a
 
      05-17-2006
I would learn basic, commandline SQL first and get comfortable creating
tables, querying your dbs etc. THEN, add Python. Otherwise you spin
your wheels not knowing whether it's your use of the Python modules or
your bad SQL commands that are fouling things up.

http://sqlcourse.com/intro.html

or I recommend Chris Fehily's SQL 2nd Ed. Great book, and cheap.

 
Reply With Quote
 
John Salerno
Guest
Posts: n/a
 
      05-17-2006
Heiko Wundram wrote:

> Of course, you need to log on with a user who is allowed to create databases.


yeah, this is where I'm stuck. The only "logging on" i know how to do is
with the connect method, but that requires a database to exist already
 
Reply With Quote
 
John Salerno
Guest
Posts: n/a
 
      05-17-2006
BartlebyScrivener wrote:
> I would learn basic, commandline SQL first and get comfortable creating
> tables, querying your dbs etc. THEN, add Python. Otherwise you spin
> your wheels not knowing whether it's your use of the Python modules or
> your bad SQL commands that are fouling things up.
>
> http://sqlcourse.com/intro.html
>
> or I recommend Chris Fehily's SQL 2nd Ed. Great book, and cheap.
>


I did that tutorial yesterday. It was great and I learned a lot about
the basics of working with tables. After learning the queries, then I
moved on to using them with Python. I plan to get the pocket reference
later today, so that might help as well.
 
Reply With Quote
 
Jesse Hager
Guest
Posts: n/a
 
      05-18-2006
John Salerno wrote:
> Since the connect method of mysqldb requires a database name, it seems
> like you can't use it without having a database already created. So is
> there a way to connect to your mysql server (without a specified
> database) in order to create a new database (i.e., the CREATE DATABASE
> query)?
>
> Thanks.


In every MySQL library I have ever seen, the database parameter is
optional. You may either omit it or pass an empty string. It is just a
shortcut so the application does not need to send a "USE" command to
select the active database.

>>>import MySQLdb
>>>db = MySQLdb.connect("host","username","password")
>>>c = db.cursor()


To get the currently selected database:

>>>c.execute("SELECT DATABASE()")

1L
>>>c.fetchall()

((None,),)
^^^
None (NULL) indicates no currently selected database.

To set the default database:

>>>c.execute("USE mysql")

0L

Getting the database again:

>>>c.execute("SELECT DATABASE()")

1L
>>>c.fetchall()

(('mysql',),)
^^^
A string indicates that a database is currently selected.

Hope this helps.

--
Jesse Hager
email = "".decode("rot13")
 
Reply With Quote
 
John Salerno
Guest
Posts: n/a
 
      05-18-2006
Jesse Hager wrote:

> In every MySQL library I have ever seen, the database parameter is
> optional. You may either omit it or pass an empty string. It is just a
> shortcut so the application does not need to send a "USE" command to
> select the active database.


I tried it without the db parameter, then sent a CREATE TABLE query, but
I got this:

OperationalError: (1046, 'No Database Selected')
 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Database Database Database Database scott93727@gmail.com Computer Information 0 09-27-2012 02:43 AM
DataBase DataBase DataBase DataBase scott93727@gmail.com Computer Information 0 09-26-2012 09:40 AM
Obscure MySQLdb question - duplicating a database handle John Nagle Python 2 09-03-2010 06:39 AM
Javascript new-new-new-new-newbee weblinkunlimited@gmail.com Javascript 2 03-11-2008 01:15 AM
Creating new database 'Views' in Server Explorer =?Utf-8?B?U2V2ZW4=?= ASP .Net 2 10-29-2006 04:23 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57