creating a new database with mysqldb

J

John Salerno

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.
 
B

BartlebyScrivener

Type:

create a new database with mysql

into google and see what happens

rd
 
J

John Salerno

BartlebyScrivener said:
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.
 
P

Philippe Martin

John said:
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
 
H

Heiko Wundram

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.
 
B

BartlebyScrivener

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.
 
J

John Salerno

Heiko said:
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
 
J

John Salerno

BartlebyScrivener said:
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.
 
J

Jesse Hager

John said:
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.

To get the currently selected database:
((None,),)
^^^
None (NULL) indicates no currently selected database.

To set the default database:
0L

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

Hope this helps.
 
J

John Salerno

Jesse said:
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')
 
B

BartlebyScrivener

I was hoping you'd find this earlier when I suggested that you type:

creating a new database with MySQL

into google.

It's the number one hit:

http://coronet.iicm.edu/mysql/create.html

If you send the commands listed there via the commandline or through
MySQLdb you should be in business.
 
D

Dennis Lee Bieber

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

OperationalError: (1046, 'No Database Selected')

Well, as it says... You have to be in some database before you can
create TABLEs... Your thread started with wanting to do "create
database" (which I suspect you won't have privilege for on that shared
hosting service).
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
J

John Salerno

BartlebyScrivener said:
I was hoping you'd find this earlier when I suggested that you type:

creating a new database with MySQL

into google.

It's the number one hit:

http://coronet.iicm.edu/mysql/create.html

If you send the commands listed there via the commandline or through
MySQLdb you should be in business.

I saw this one, but it seems to use a lot of the command line, and I
didn't know how all that translates into a Python script. My goal was to
do it all using just mysqldb, but maybe it's just easier (or more
normal) to create a database in another way, and *then* use mysqldb to
manipulate it.
 
D

Dennis Lee Bieber

I suspect the related link won't help. The MySQL server isn't one
under J.S.'s ownership control (as I understand it) but is a shared
hosting provider.
I saw this one, but it seems to use a lot of the command line, and I
didn't know how all that translates into a Python script. My goal was to
do it all using just mysqldb, but maybe it's just easier (or more
normal) to create a database in another way, and *then* use mysqldb to
manipulate it.

From the linked article:

m> First off all we connect to the MySQL server in the way described in
the last section:
m>
m> > mysql -u root -p
m>
m> The user will be prompted for the password:
m>
m> Enter password: *****

Those lines invoke the simple command-line command processor for
MySQL. They are the equivalent of:

con = MySQLdb.connect(user="root", passwd="*****", host="localhost")
#note, no "db="

crs = con.cursor()

m> mysql> use mysql;
m>

crs.execute("use mysql")
#or use 'db="mysql"' on the connect call


Anything the site shows with a "mysql> " prompt is a string that can
be fed to .execute(). If the command returns data (other than the # rows
affected) you need a .fetch???() call to retrieve the data.


The main problem is that the site assumes you are the
"superuser/root/DBA" for the server. IOWs, you are the person who can
create accounts for other people, startup/shutdown the server, and do
/anything/ to /any/ database the server controls.

As a client on a shared host, I suspect the provider created a user
account and A (one) database for that account. The privilege tables for
that user account would have been set so that only that database is
available. The account would have full privileges within that database,
and no privileges to access any other database (including the mysql
master tables). Depending the strictness of the provider, the account
may or may not allow for connections from outside hosts, or only from
"localhost" (ie, CGI applications that are running on the provider's
machine, and not via "mysql -u user -h somewhere.out.there -p" or
programmatic equivalents)



--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
J

John Salerno

Dennis said:
As a client on a shared host, I suspect the provider created a user
account and A (one) database for that account. The privilege tables for
that user account would have been set so that only that database is
available. The account would have full privileges within that database,
and no privileges to access any other database (including the mysql
master tables). Depending the strictness of the provider, the account
may or may not allow for connections from outside hosts, or only from
"localhost" (ie, CGI applications that are running on the provider's
machine, and not via "mysql -u user -h somewhere.out.there -p" or
programmatic equivalents)

I know for sure I can't connect from anywhere else, only the server
itself. But I *think* I have full privileges to do what I want with the
database, but I need to do some further checking, I suppose. It says I
have 'amdmin privilegs' but that might not be the same as what I need to
create databases outside of the control panel.
 
D

Dennis Lee Bieber

database, but I need to do some further checking, I suppose. It says I
have 'amdmin privilegs' but that might not be the same as what I need to
create databases outside of the control panel.

<G> I sure hope it says "admin privileges" -- otherwise I'd advise
you to run away from that provider.

If this is an HTTP based control panel, it means they've already
filtered things down to what user's are limited to... Again, probably
giving you access ONLY to what is inside the "database" directory
created for your account.


FYI: Another book you might want to look for is from SAMS, sort of
orangish cover: MySQL Crash Course

Covers up to MySQL 5.x (stored procedures, etc.). Only a $20 book
(and my copy has a 30% off sticker on top!)
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
F

Frithiof Andreas Jensen

John Salerno said:
Since the connect method of mysqldb requires a database name, it seems
like you can't use it without having a database already created.

The web hotel I use create *one* database together with the account.

I.O.W:

I cannot create any databases; I can create/delete as many tables as I
like within the database (up to the disk space that I have). This is
pretty normal, I think.
 
J

John Salerno

Frithiof said:
The web hotel I use create *one* database together with the account.

I.O.W:

I cannot create any databases; I can create/delete as many tables as I
like within the database (up to the disk space that I have). This is
pretty normal, I think.

No, I can create 25 databases.
 
D

Dennis Lee Bieber

No, I can create 25 databases.

From what I saw in earlier posts... The database creation is
probably being handled by code running in the provider's workspace -- so
/it/ can control setting up access restrictions (and your count limit).
Your account in MySQL, itself, did not have "create database" privilege
-- by running through the provider web form, the "create database" was
run by "their" account, not yours.
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top