Hi,
I don't know if web2py can do that but i have a python script which create
the define_table statements.
You have to execute with the terminal and python (on windows : python
yourscript.py).
And for the primary key in the table, you have to modify the type (put id
instead of int).
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
mysqltoweb2py --> Import an already existing MySQL Database
into web2py.
Usage:
mysqltoweb2py.py <database> <userid> <passwd>
'''
# import the necesssary modules :)
import sys
import re
import MySQLdb
reFieldType = re.compile(r'([a-zA-Z]+)([(]\d+[)])?')
reLetter = re.compile('[a-zA-Z]')
# my args
#datab = sys.argv[1]
#user = sys.argv[2]
#passwd = sys.argv[3]
db=MySQLdb.connect(host='localhost',user= "user", passwd = "password", db =
"database")
def error_msg(msg):
''' This could be implemented as an exception
'''
sys.stderr.write("%s\n" % msg)
sys.exit(1)
def output_table(table):
cursor=db.cursor()
cursor.execute('show columns from `%s`' % table)
print "db.define_table('%s'," % table
# Extract table fields
for field in cursor.fetchall():
if field[0] == 'id':
continue # id field ignored
if not reLetter.match(field[0][0]):
error_msg("Error: field name [%s] in table [%s] does not begin
with a letter" % (field[0], table))
ftype = reFieldType.match(field[1])
if not ftype:
error_msg("Error: could not parse column type")
_type, _len = tuple(ftype.groups())
if _type == 'varchar':
_type = 'string'
elif _type in ('int', 'tinyint'):
_type = 'integer'
print "\tField('%s'," % field[0],
print "type = '%s'" % _type,
if _len is not None: # Has a length?
print ", length = %i" % int(_len[1:-1]),
print "),"
print "\tmigrate = False)"
cursor = db.cursor()
cursor.execute('show tables')
for table in cursor.fetchall():
print
output_table(table[0])
Le mercredi 6 janvier 2016 04:09:07 UTC+1, Nir Haramati a écrit :
>
> Can I import an already designed database into web2py?
>
> Put differently, can I instruct web2py to generate a model page with
> define_table statements for existing tables in a database?
>
--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.