Python Operations Remote Database

Source: Internet
Author: User

My project to insert create_time and update_time into the database, it is bound to refer to the current system time, after a large number of lookups, and finally found that there is no corresponding time for Python to the relevant wildcard characters, then how can we achieve it.

It's really simple, we just need to convert the datetime to a string type.

    def insertintochannel (self, user):              conn = jdbcutils.getconnection ()              cursor = conn.cursor ()              dt= Datetime.datetime.now (). Strftime ("%y-%m-%d%h:%m:%s")              cursor.execute ("INSERT into user (Id,age,name,create_ Time,update_time) \                            values ('%d ', '%d ', '%s ', '%s ', '%s ') "% \                             (User.getid (), User.getage (), User.getname (), DT, DT))              cursor.close ()              conn.commit ()              conn.close ()  

Time interval

Import Timedef Sleeptime (hour,min,sec):    return hour*3600 + min*60 + sec;second = sleeptime (0,0,20); while 1==1:    Time.sleep (second);    print ' do action ' #这是隔20秒执行一次;

Additionally, operating the remote database requires local authorization from the remote database:

If not authorized, the following prompt will appear

After you log in to MySQL, change the "host" item in the "User" table in the "MySQL" Database and rename it from "localhost" to '% '.

Mysql-u root-pmysql>use mysql;mysql>select ' host ' from user where user= ' root '; mysql>update user set host = '% ' where user = ' root '; Mysql>flush privileges;mysql>select ' host '   

The first sentence is to log in as the user root with permissions

Second sentence: select MySQL Library

The third sentence: View the host value of the user table in the MySQL library (hosts/IP names that can be accessed by the connection)

Clause Four: Modify the host value (increase the hostname/IP address with the wildcard%), or you can directly add

Clause five: Refresh MySQL system permissions related table

The sixth sentence: when you re-view the user table, there are modifications.

Remember that the MySQL service needs to be restarted (make sure the changes are valid), otherwise the result of the modification may not be reflected.

Note: The local root account cannot connect to the database after the modification

PhpMyAdmin tries to connect to the MySQL server, but the server rejects the connection. You should check the host, user name, and password in the configuration file and confirm that the information is consistent with the information given by the MySQL server administrator.

Still logged in with the root account, the password is empty. Delete the root user and then create and authorize.

Mysql-u root-pdrop user ' root ' @ ' localhost ' create user ' root ' @ ' localhost ' identified by ' your password '; grant all privileges on *. * to [email protected] ' localhost ';

Alternatively: You can create a remote login user and authorize

Grant all privileges the test_db.* to [email protected] ' 192.168.1.101 '  

All privileges means giving all permissions to the specified user, which can also be replaced by assigning a specific permission, such as Select,insert,update,delete,create,drop, to be separated by a "," half-width comma between specific permissions.

Test_db.* means that the above permissions are for which table, test_db refers to the database, followed by the * for all tables, it can be inferred that: for all of the database is authorized as "*. *" For all tables, the entire table for a database is authorized as "database name. *", A table for a database is authorized as the database name. Table name.

Root indicates which user you want to authorize, and the user can be either an existing user or a non-existent user.

192.168.1.101 represents the IP address that allows remote connections, that is, your local IP address. If you want to not restrict the IP of the link, set to "%".

123456 is the user's password.

ERROR: (2006, ' MySQL server has gone away ')

Problem Analysis: The link between client and MySQL server is broken.

The reason for this is generally that the SQL operation takes too long, or that the data being transferred is too large (for example, using INSERT ...) the statement of values is too long, which can be avoided by modifying the configuration parameters of the max_allowed_packed, or by inserting the data in batches in the program.

Python Operations Remote 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.