SoFunction
Updated on 2024-11-13

How to implement query and other operations on the database through pycharm (non-multistep operation)

Pycharm's querying of databases and other operations (non-multistep operations)

import pymysql
import pandas as pd

connect = (host="IP",port=3306,user="user",password="password",database="database",charset="utf8")
cur = ()
sql = 'Database Statements '
# of data items queried
res = (sql)
print(res)
# 4. Getting the queried data
# Method 1: Get a query data
data_all = ()
print(data_all)

Steps to manipulate the database in Pycharm

Recently, there are always friends ask me about the database of some of the problems, although the problem is small, but the solution is very troublesome, take the time to organize a bit, including most of the problems to the "student achievement database" example of the way to show. Here, assuming that you have prepared a server-side, client-side

Server Configuration

Take Ubuntu 20.04 for example, first you need to configure the server, mysql installation is omitted here:

1. Enable MySQL Remote Connection

Change Configuration File,restart sth.!
 /etc/mysql/
 vi   locate43around,plus # Note
   # bind-address = 127.0.0.1
   (Alternatively, the,127.0.0.1change to:0.0.0.0)

3.Save Exit
 service mysql restart
5.go intomysqlModify the user tablehost(be) worth
  use mysql;
  update user set host='%' where user='root';
6.refresh access
  flush privileges;
 (Actually, the new version,No need to refresh,But it's always good to brush up.,It'll take care of any unnecessary trouble.)

2. Add authorized users

1. expense or outlayrootexpense or outlay户登录mysql
   mysql -u root -p
2. 添加expense or outlay户 % a meter (measuring sth)示自动选择可expense or outlayIP
   CREATE USER 'username'@'host' IDENTIFIED BY 'password';
3. Rights Management

   # Increase permissions
   grant permission list on storehouse.a meter (measuring sth) to "Username"@"%"  with grant option; 
   //Note: Newer versions do not require the field: identified by "password".
   // If it means all tables under the library: library. *
   // If it means all tables under all libraries: *. *
   //If multiple libraries and tables are represented:,c,d
   //with grant option means that you can assign pre-existing privileges to subordinates.

   # Delete permissions
   revoke insert,update,select on storehouse.a meter (measuring sth) from 'user'@'%';
   4. refresh access
   flush privileges;
5. 删除expense or outlay户
   drop user "Username"@"%"

 
#Privilege list


all privileges 、select 、insert ,update,delete,alteret al. (and other authors)。
storehouse.a meter (measuring sth) : *.* 代a meter (measuring sth)所有storehouse的所有a meter (measuring sth)

Example:

1. Create User
  mysql>create user  'yym'@'%'  identified by '123';
2. Adding Authorized Usersyym,cryptographic123,All rights to all tables in all libraries
  mysql>grant all privileges on *.* to 'yym'@'%'  with grant option;
  mysql>flush privileges;
3. Add Userduty,cryptographic123,treat (sb a certain way)textAll tables in the library have view,Insertion Privileges
  mysql>grant select,insert on text.* to 'duty'@'%'  with grant option;
  mysql>flush privileges;
4. removingyym用户的removing权限
  mysql>revoke delete on *.* from "yym"@"%";
5. removing用户duty
  drop user "duty"@"%";

Client Configuration

1. First familiarize yourself with the pymysql usage process

  • Establish database connection (db = (...))
  • Create cursor object (cur = ())
  • Cursor method: ("insert ...")
  • Submit to database or get data : ()/()
  • Close cursor object :()
  • Disconnect from database :()

build

import pymysql

database = {
    "host": "180.76.***.***",
    "port": 3306,
    "user": "yym (user created above)",
    "password": "123456 (the password here, not the server password, but your database password)",
    "database": "text (the specified database)",
    "charset": "utf8"
}
db = (**database) / / double star combined reference, do not recognize the Baidu asterisk pass reference
cur = ()
try:
    name_stu=input("Enter student's name")
    sql = "update cls set score=%s where name=%s;"
    (sql,[1000,name_stu])
    ()
except Exception as e:
    print(e)
    ()
()
()

The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.