I have the following SQL file:BEGIN TRANSACTION;CREATE TABLE topic(id INTEGER PRIMARY KEY AUTOINCREMENT,title CHAR(512) NOT NULL,is_active CHAR(1),created_on TIMESTAMP,created_by INTEGER REFERENCES auth_user (id) ON DELETE CASCADE,modified_on TIMESTAMP,modified_by INTEGER REFERENCES auth_user (id) ON DELETE CASCADE);INSERT INTO `topic` VALUES(1,'regergerg','T','2015-04-14 14:32:52',1,'2015-04-14 14:32:52',1);INSERT INTO `topic` VALUES(2,'egergerger','T','2015-04-14 14:33:27',1,'2015-04-14 14:33:27',1);Is there a script that will convert this to db.py in other words, spit out the following code?db.define_table('topic',Field('title', label=T('Title'), notnull=True, requires=IS_NOT_EMPTY()),auth.signature,format='%(title)s',)
--I asked because if I have a large SQL file then creating the table by hand or looking at the schema may take some time. Wondering if there is a script in the script folder that automoatically does this conversion. I know "extract_mysql_models.py" in the script folder but that only take an existing mysql file and does the conversion. I am not sure if it does SQL to DAL directly. Am I correct?
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Yes I did. I forgot to add. The extract_sqlite_models.py didn't work as expect. And that may be because I am not doing it right. Thats why I didn't mention.
Can you confirm if it was working before? I just don't know if it was.
I tried this:db = DAL('mysql://ro...@127.0.0.1:8000/my_database_name',migrate_enabled=False, pool_size=20)It didn't work either. I guess someone needs to look at how to connect to legacy database.
import MySQLdb
import sys
try:
db = MySQLdb.connect(host = 'localhost',user ='root',passwd = ' ',db = 'my_dabasename')
except Exception as e:
sys.exit('we cant get into the db');
cursor = db.cursor()
cursor.execute('SELECT *FROM table')
results = cursor.fetchall()
print results
Once the table is fetched then use the insert_into_table option to create db object.
But I agree with Niphlod, import, export csv is probably the only way to go around, if it works:-). And also agree with richard. sqlbroser does take the storage object as input and can export csv table and then use that back to create the database. I was just hoping to directly connect to my local server (WAMP) where I have the mysql database defined. I was under the impression, db = DAL('mysql://ro...@127.0.0.1:8000/my_database_name',migrate_enabled=False, pool_size=20) is the way to go about it.
This is what I was looking for.But homehow the code didn't work for me even when I followed the example like it says. So, I am creating a stand alone version that works with MySQLdb.
p = subprocess.Popen(['mysqldump','--user=%s' % username,'--password=%s' % password,'--skip-add-drop-table','--no-data', database_name,table_name[0]], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
sql_create_stmnt, stderr = p.communicate()
I don't know what a mysql dump is. Wondering if the above two lines can be written like,
cat_str = 'SELECT *From' + " " + table_name[0]
get_table = cursor.execute(cat_str)
sql_create_stmnt = cursor.fetchall()
And then go on with if 'CREATE' in sql_create_stmnt:
Anyway, I am attaching the script if it make it makes it any clear.
This plugin needs: | |
mysql | |
mysqldump | |
installed and globally available. | |
Under Windows you will probably need to add the mysql executable directory to the PATH variable, | |
you will also need to modify mysql to mysql.exe and mysqldump to mysqldump.exe below. | |
Just guessing here :) | |
Access your tables with: | |
legacy_db(legacy_db.mytable.id>0).select() | |
If the script crashes this is might be due to that fact that the data_type_map dictionary below is incomplete. | |
Please complete it, improve it and continue. |
cat_str = 'SELECT *From' + " " + table_name[0]
get_table = cursor.execute(cat_str)
sql_create_stmnt = cursor.fetchall()