cx_oracle
Recently I’ve been working a lot with Oracle Databases and Python. Although Oracle is a big company, I found there’s not such a strong community to help with their package in Python for dealing with databases, cx_Oracle, and I found their documentation not that great.
最近,我一直在与Oracle数据库和Python一起工作。 尽管Oracle是一家大公司,但我发现并没有一个强大的社区来帮助他们使用Python处理数据库cx_Oracle的软件包,而且我发现他们的文档不是很好。
So, I decided to do this post following my philosophy of writing posts that the Thiago (me) of 1 month ago would have thanked for.
因此,我决定按照我的写作哲学来写这篇文章,这是1个月前的Thiago(我)会感谢的。
1.连接到数据库 (1. Connect to the database)
The first thing to do is to connect to your database. In this case, probably you have an user and a password. Also, you should have the data source name. With all of those in hand you can connect to the database:
首先要做的是连接到数据库。 在这种情况下,您可能有一个用户和一个密码。 另外,您应该具有数据源名称。 有了这些,您可以连接到数据库:
Easy peasy!
十分简单!
2.执行SQL命令 (2. Execute SQL Commands)
With cx_Oracle you can execute any Oracle SQL command, selects, inserts, updates etc. In order to execute the commands, you have to create a cursor. You can use the same cursor for running several commands. I just recommend you to use a connection and a cursor for each function in your application. So, you create a connection and a cursor in the beginning of your function and close them at the end.
使用cx_Oracle,您可以执行任何Oracle SQL命令,选择,插入,更新等。为了执行命令,必须创建一个游标。 您可以使用同一光标运行多个命令。 我只建议您为应用程序中的每个功能使用连接和游标。 因此,您可以在函数的开头创建一个连接和一个游标,然后在结尾处将其关闭。
2.1选择语句 (2.1 Select Statements)
It is important to mention here that the fetchone() and