简体   繁体   中英

is there an easy/efficient way to make a mysql database dynamically 'build it's own structure' as it saves name/value pairs to disc?

is there any efficient and easy way to execute statements similar to:

CREATE TABLE IF NOT EXISTS fubar ( id int, name varchar(80))

for columns as you perform insert statements.

I'd imagine it will be a lot more complicated, but just for the sake of explanation, i guess I'm looking for something like....

IF NOT EXISTS
(
  SELECT * FROM information_schema.COLUMNS
  WHERE 
    COLUMN_NAME='new_column' AND 
    TABLE_NAME='the_table' AND 
    TABLE_SCHEMA='the_schema'
)
THEN
  ALTER TABLE `the_schema`.`the_table`
  ADD COLUMN `new_column` bigint(20) unsigned NOT NULL default 1;

alternatively, is there a way a python library that might handle the process?

basically, i want the 'id's' of a 'dictionary' to define the columns, and create them if they do not already exist.

i also would like the database to stay reasonably efficient, so i guess some dynamic handling of the type of data would also be necessary?

just wondering if anything like this exists at the moment, and if not looking for advice on how best to achieve it....

I believe that you're looking for an Object Relational Mapping system.

For Python there are a couple available:

Or if you're building a website, the Django project includes an ORM system: http://www.djangoproject.com/

To simplify the process, you can add something like Elixir on top of SQLAlchemy: http://elixir.ematia.de/trac/wiki

You would get code like this:

class Movie(Entity):
    title = Field(Unicode(30))
    year = Field(Integer)
    description = Field(UnicodeText)

This is how to insert:

>>> Movie(title=u"Blade Runner", year=1982)
<Movie "Blade Runner" (1982)>
>>> session.commit()

Or fetch the results:

>>> Movie.query.all()
[<Movie "Blade Runner" (1982)>]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM