In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Although based on the command line + text editing can complete python program development, and familiar with the command operation under the operating system. But development based on integrated development environment can be more efficient. Therefore, it is important to choose a suitable integrated development tool. The initial time is also from the command line + text editing to develop, so that you can be familiar with the basic command operations. Then I tried to use eclipse+pyDev,pycharm. Relatively speaking, pycharm is simpler.
1. Install pycharm
Click on the .exe file. Resources_cn.jar is a Chinese package and copy it to lib under the installation directory.
two。 Install the plug-in
Take the database plug-in as an example, this plug-in can easily operate the database and greatly improve the efficiency of developers to view operational data.
Figure 2-1
Click the Settings button and Plugins can view the plug-in.
Figure 2-2
Open the database window, click the + button, and select oracle or another database.
Figure 2-3
Figure 2-4
Set information such as the database host to which you need to connect. You can connect to the database directly in the tool.
3. Python connects to oracle
Because developers or data analysts need to read the data in the database for more complex operations. Python provides a cx_Oracle module, which can be imported to enable python programs to connect to the database and operate on the data in the database.
(1) download cx_Oracle
Python is an official website, PyPI, with a wealth of modules. Cx_Oracle can be downloaded from PyPI. Open PyPI's URL https://pypi.python.org/pypi and search cx_Oracle inside to find the module, which can be downloaded from http://cx-oracle.sourceforge.net/. Of course, it can also be downloaded through other ways. But only 5.1.2, corresponding to python3.3, was found on the home page of this site. With the idea that it might be compatible. After downloading and installing, it always reports an error: ImportError: DLL load failed: the specified program cannot be found. According to the online method, putting oci.dll,oraociei11.dll,oraocci11.dll into pytho34/lib/site-packages still reports an error. Later, a version supporting 3.4 was found on https://pypi.python.org/pypi/cx_Oracle/5.1.3. It will be ready after installation.
(2) now you can try to connect to the database:
Import cx_Oracle # reference module cx_Oracle
Conn=cx_Oracle.connect ('load/123456@localhost/ora11g') # connect to the database
C=conn.cursor () # get cursor
X=c.execute ('select sysdate from dual') # uses cursor for various operations
X.fetchone ()
Print (x)
C.close () # close cursor
Conn.close () # close the connection
(3) function introduction, quoted from http://blog.sina.com.cn/xnlza. Although it is to operate mysql, the functions should be similar.
Callproc (self, procname, args): used to execute a stored procedure. The parameters received are the stored procedure name and parameter list, and the return value is the number of affected rows.
Execute (self, query, args): executes a single sql statement. The parameters received are the sql statement itself and the list of parameters used, and the return value is the number of affected rows.
Executemany (self, query, args): executes a single sql statement, but repeats the parameters in the parameter list. The return value is the number of affected rows.
Nextset (self): move to the next result set
4. The method used by cursor to receive the return value:
Fetchall (self): receives all the returned result rows.
Fetchmany (self, size=None): receives the size bar and returns the result row. If the value of size is greater than the number of result rows returned, cursor.arraysize data is returned.
Fetchone (self): returns a result row.
Scroll (self, value, mode='relative'): move the pointer to a row. If mode='relative', means moving the value bar from the current row, if mode='absolute', means moving the value bar from the first row of the result set.
5. The following code is a complete example.
# use the SQL statement, where the parameters to be received use the% s placeholder. It is important to note that no matter what type of data you want to insert, the placeholder will always use% s
Sql= "insert into cdinfo values (% SJM% s)"
# param should be tuple or list
Param= (title,singer,imgurl,url,alpha)
# execute, if successful, the value of n is 1
N=cursor.execute (sql,param)
# Let's perform a query operation
Cursor.execute ("select * from cdinfo")
# We use the fetchall method. In this way, all the results returned by the query will be saved in the cds. Each result is a data of type tuple, and these tuple make up a tuple
Cds=cursor.fetchall ()
# because it is tuple, you can use the result set in this way
Print cds [0] [3]
# or show it directly to see what the result set really looks like
Print cds
# if you need to insert data in bulk, do so
Sql= "insert into cdinfo values (0JI% SJM% sJM% sJet% sJet% s)"
# each set of values is a tuple, and the whole parameter set forms a tuple, or list
Param= ((title,singer,imgurl,url,alpha), (title2,singer2,imgurl2,url2,alpha2))
# use the executemany method to insert data in bulk. This is really a cool way!
N=cursor.executemany (sql,param)
After performing the insert or delete or modify operation, you need to call the conn.commit () method to submit.
6. Close the database connection
You need to close the pointer object and connect the object respectively. They have the same name.
Cursor.close ()
Conn.close ()
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.