Exemple #1
0
By consequence, to manipulate fields of a SQL row, you just need to read/write a Python variable (class member). 


"""

import sys
sys.path.append('..')

if __name__ == '__main__':

    from OpenElectrophy import open_db

    # connection to a DB
    url = 'sqlite:///test.sqlite'
    open_db(url=url, myglobals=globals(), use_global_session=True)

    # create an empty segment variable
    seg1 = Segment()

    # fill attributes (=columns in SQL)
    seg1.name = 'My first segment'
    seg1.description = 'This is just a test'
    seg1.index = 3

    # TEXT field in SQL = str in python
    print(seg1.name)
    print(type(seg1.name))

    print(seg1.id)
    # give None
"""

import sys
sys.path.append('..')


if __name__== '__main__':

    from OpenElectrophy import open_db, sql, neo_to_oe, TryItIO
    import datetime
    import numpy


    # connection to a DB
    open_db( url = 'sqlite:///test.sqlite', myglobals= globals(), use_global_session = True)


    # create 5 Blocks with different dates
    for i in range(5):
        neo_bl = TryItIO().read(nb_segment = 2, duration = 2)
        bl = neo_to_oe(neo_bl, cascade = True)
        bl.rec_datetime = datetime.datetime(2012, 06, 1+i, 12,30,40)
        bl.name = 'test sql %d' %i
        bl.save()
        print bl


    # select all Block.id after 3 june 2012 and Segment name is 1
    query = """
                SELECT Block.id, Segment.id, AnalogSignal.id
"""
Update 
------------------

If you want to update a row, it is easy:
 * Load an object
 * modify its attributes
 * Save the object


"""

import sys
sys.path.append('..')

if __name__== '__main__':

    from OpenElectrophy import open_db
    # connection to a DB
    open_db( url = 'sqlite:///test.sqlite', myglobals= globals(), use_global_session = True)

    # I know the ID
    id= 2
    seg = Segment.load(id)

    # Modify attribute
    seg.name = 'modified name'

    # save 
    seg.save()
"""

import sys
sys.path.append('..')


if __name__== '__main__':

    from OpenElectrophy import open_db, OEBase
    from OpenElectrophy.io import TryItIO # come from neo
    import numpy as np
    import quantities as pq
    

    # connection to a DB 1
    dbinfo1 = open_db( url = 'sqlite:///test1.sqlite', myglobals=None, use_global_session = False)
    session1 = dbinfo1.Session() # create a sqlalchemy session
    classes1 = { }
    for c in dbinfo1.mapped_classes:
        classes1[c.__name__] = c
    # You can do this
    Block = classes1['Block']
    bl = Block(name = 'yep')
    # or this
    bl = classes1['Block'](name = 'yep')
    
    # This allow you to play with 2 databases
    # connection to a DB 2
    dbinfo2 = open_db( url = 'sqlite:///test2.sqlite', myglobals=None, use_global_session = False)
    classes2 = { }
    for c in dbinfo2.mapped_classes:
By consequence, to manipulate fields of a SQL row, you just need to read/write a Python variable (class member). 


"""

import sys

sys.path.append("..")

if __name__ == "__main__":

    from OpenElectrophy import open_db

    # connection to a DB
    url = "sqlite:///test.sqlite"
    open_db(url=url, myglobals=globals(), use_global_session=True)

    # create an empty segment variable
    seg1 = Segment()

    # fill attributes (=columns in SQL)
    seg1.name = "My first segment"
    seg1.description = "This is just a test"
    seg1.index = 3

    # TEXT field in SQL = str in python
    print seg1.name
    print type(seg1.name)

    print seg1.id
    # give None
"""

import sys

sys.path.append("..")


if __name__ == "__main__":

    from OpenElectrophy import open_db, OEBase
    from OpenElectrophy.io import TryItIO  # come from neo
    import numpy as np
    import quantities as pq

    # connection to a DB 1
    dbinfo1 = open_db(url="sqlite:///test1.sqlite", myglobals=None, use_global_session=False)
    session1 = dbinfo1.Session()  # create a sqlalchemy session
    classes1 = {}
    for c in dbinfo1.mapped_classes:
        classes1[c.__name__] = c
    # You can do this
    Block = classes1["Block"]
    bl = Block(name="yep")
    # or this
    bl = classes1["Block"](name="yep")

    # This allow you to play with 2 databases
    # connection to a DB 2
    dbinfo2 = open_db(url="sqlite:///test2.sqlite", myglobals=None, use_global_session=False)
    classes2 = {}
    for c in dbinfo2.mapped_classes:
For more detail see open_db.


"""

import sys
sys.path.append('..')

if __name__== '__main__':

    from OpenElectrophy import open_db
    # connection to a DB
    #url = 'sqlite:////home/sgarcia/test.db'
    #url = 'sqlite:///C:/Users/sgarcia/Desktop/test.db'
    #url = 'sqlite:///test.db'
    #open_db( url = url)
    
    # this open mysql
    url = 'mysql://*****:*****@neuro001.univ-lyon1.fr/test_dev_1'
    open_db( url = url, myglobals= globals(), use_global_session = True)

    # this open sqlite
    open_db( url = 'sqlite:///test.db', myglobals= globals(), use_global_session = True)

    # this open sqlite + pytables (hdf5)
    url = 'mysql://*****:*****@neuro001.univ-lyon1.fr/test_dev_1'
    open_db( url = url, myglobals= globals(), use_global_session = True, hdf5_filename = 'test1.h5')