Using SQLAlchemy to manipulate the Firebird database

Source: Internet
Author: User
Tags firebird database interbase gsec

Come to this company has been a week, spent the beginning of the boring days ready to formally do something, these days contacted the file database InterBase, try to connect their open source version Firebird on Ubuntu, because the company uses SQLAlchemy, So I'm going to use Python to illustrate the example.

#安装firebird数据库

sudo apt-get install Firebird-super

2. Change the password for the database administrator sysdba in Firebird:

sudo dpkg-reconfigure firebird2. 5-super

3, use the GSEC command to detect the installation success:

Gsec-user Sysdba-password Masterkey

If you can enter, it means Firebird installation is successful.

4. Use ISQL-FB to create a new database:

isql-fbsql'/tmp/db_name.gdb'sysdba' Masterkey '

5, install the graphical interface of Firebird database Flamerobin

6,python using the SQLAlchemy framework to connect to the database: Installing the Fdb-1.4.1.tar library in use

 fromSqlalchemy.ormImportMapper, Sessionmaker fromSQLAlchemyImportCreate_engine, Table, Column, Integer, String, MetaData fromSqlalchemy.sql.expressionImportCast fromSqlalchemy.ext.compilerImportcompiles fromSqlalchemy.dialects.mysqlImportBIGINT, BINARY, BIT, BLOB, BOOLEAN, CHAR, DATE, DATETIME, Decimal, Decimal, DOUBLE, ENUM, FLOAT, INTE  GER, Longblob, Longtext, Mediumblob, Mediumint, Mediumtext, NCHAR, NUMERIC, NVARCHAR, REAL, SET, SMALLINT, TEXT, Time, TIMESTAMP, Tinyblob, TINYINT, Tinytext, VARBINARY, VARCHAR, yearImportSQLAlchemy as SA#the above is the guide packagemetadata=MetaData () usertable=Table ("Jonny_user", metadata, Column ('user_id', Integer, primary_key=True), Column ('user_name', VARCHAR (+), Unique=true, nullable=False), Column ('Password', VARCHAR (+), nullable=True))#The following database is a statement that connects to the MySQL database#mysql_db = Create_engine (' mysql://root:[email protected]:3306/jonny ')#The following sentence is connected to the Firebird database statement, the default account of the database is ' SYSDBA ' default password is ' Masterkey 'Dburl=sa.engine.url.url ('Firebird', username='SYSDBA', password='Masterkey', database='/tmp/firstdb3.gdb') mysql_db=sa.create_engine (dburl,encoding='gb2312', echo=False) Metadata.create_all (mysql_db)#Create user ClassclassUser (object):Pass#Association classes and TablesMapper (User, usertable)#Get SessionSession =Sessionmaker () session.configure (Bind=mysql_db) Session=Session ()#Main functiondefMain (): Inputtypef= Raw_input ("InputType:")    ifINPUTTYPEF = ='Insert': Insert ()elifINPUTTYPEF = ='Find': Find ()elifINPUTTYPEF = ='Delete': Delete ()Else:       Print 'Error'#Find Methoddeffind ():Print 'name'name= Raw_input ("Name:")     forInstanceinch(Session.query (User). Filter_by (user_name=name). All ()):PrintInstance.user_name, Instance.password#Delete MethoddefDelete ():Print 'name'name= Raw_input ("Name:") (Session.query (User). filter_by (user_name=name). Delete ()) Session.flush () Session.commit ( )Print 'Deletesucceed'#Insert MethoddefInsert (): U=User () u.user_id= Raw_input ("ID:")    Print 'name'U.user_name= Raw_input ("Name:")    Print 'Password'U.password= Raw_input ("Password:") Session.add (U) session.flush () Session.commit () session.close ()if __name__=='__main__': Main ()

Use the above example to demonstrate a simple Firebird database of the function of adding and removing, first such record, for InterBase and Firebird some follow-up understanding and then record again.

Using SQLAlchemy to manipulate the Firebird database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.