473,654 Members | 3,082 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

creating a new database with mysqldb

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.
May 17 '06 #1
14 3191
Type:

create a new database with mysql

into google and see what happens

rd

May 17 '06 #2
BartlebyScriven er 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 msqladministrat or
in my server control panel, instead of using python.
May 17 '06 #3
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

May 17 '06 #4
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 msqladministrat or
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.
May 17 '06 #5
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.

May 17 '06 #6
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
May 17 '06 #7
BartlebyScriven er 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.
May 17 '06 #8
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","userna me","password ")
c = db.cursor()
To get the currently selected database:
c.execute("S ELECT DATABASE()") 1Lc.fetchall () ((None,),)
^^^
None (NULL) indicates no currently selected database.

To set the default database:
c.execute("U SE mysql") 0L

Getting the database again:
c.execute("S ELECT DATABASE()") 1Lc.fetchall ()

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

Hope this helps.

--
Jesse Hager
email = "wr********@tzn vy.pbz".decode( "rot13")
May 18 '06 #9
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:

OperationalErro r: (1046, 'No Database Selected')
May 18 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2096
by: Dave Harrison | last post by:
Hi all, got a problem combinging mx and MySQLdb, when I build and install both for my Python2.1 install on a Solaris 9 box I can import mx fine, but importing MySQLdb causing python to core dump. I am using egenenix 2.0.4 and MySQLdb 0.9.2. I have also replaced the -shared flag in the Makefile is /usr/local/lib/Python2.1/config with -G (a recommended solaris change to let the build of the modules work in the first place) -> notably I also...
1
2581
by: Peter Nikolaidis | last post by:
Greetings, I am attempting to get MySQLdb 0.9.2 installed on Mac OS 10.2 with a Fink distribution of Python 2.2.2. I have seen only a few posts on the subject, some of them relate to "conflicting header files," but I don't know what to do about conflicting header files, or where I would find them, and once I found them, which ones to remove. I have compiled MySQL 4.1 and installed into /usr/local/mysql, but since have moved to a Fink...
6
1649
by: Michael Foord | last post by:
I'm writing a couple of modules that store information about user 'behaviour'. E.g. a user accesses a certain page of a website at a certain date-time with a certain referrer. I need to analyse this information by different criteria. E.g. How many users accessed the website between August 5th and August 12th, listed by refferer !! etc It would be great to have a database engine where I could ask these questions in just a couple of...
4
3578
by: Steve Holden | last post by:
I'm trying to load module code from a database, which stores for each module its full name, code, load date and a Boolean indicating whether it's a package or not. The following simple program: import dbimp, sys if __name__ == "__main__": dbimp.install()
23
2809
by: ajikoe | last post by:
Hello I need to build table which need searching data which needs more power then dictionary or list in python, can anyone help me what kind of database suitable for python light and easy to learn. Is mySQL a nice start with python ? Sincerely Yours, Pujo
17
1935
by: John Salerno | last post by:
Ok, I've been browsing through the MySQLdb docs, and I *think* I know the kind of code I need to write (connect, cursor, manipulate data, commmit, etc. -- although I probably need to get more familiar with actual SQL commands too), but here's my problem: I don't know where these scripts are supposed to be executed, or how they are supposed to 'find' the database. Really, I have the same question for two different scenarios: accessing...
2
5218
by: Waqas.L.Khan | last post by:
Hi Guys, I'm doing some work with richtextboxes and want to be able to save and load the content from them to a mysql database (including all formatting). I figured the best way to do this would be to save the contant of the rtb to an rtf file and then load the file into a mysql BLOB field: Public Sub savetoDB()
11
3656
by: krishnakant Mane | last post by:
hello, I finally got some code to push a pickled list into a database table. but now the problem is technically complex although possible to solve. the problem is that I can nicely pickle and store lists in a blob field with the help of dumps() for picklling into a string and then passing the string to the blob. I am also able to get back the string safely and do a loads() to unpickle the object. but this only works when list contains...
0
314
by: Steve Holden | last post by:
Vaibhav.bhawsar wrote: imported The point here is that MySQLdb is a package, not a module. Some packages have their top-level __init__.py import the package's sub-modules or sub-packages to make them immediately available within the package namespace (which is why, for example, you can access os.path.* when you have imported os) and others don't. MySQLdb clearly doesn't need to import the cursors module for its own
0
8285
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8706
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8475
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7304
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5621
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4293
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1592
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.