Loop Crypto 使 web 3 中的循环事务自动化变得简单。Loop Crypto产品套件包括链上智能合约和链外工具,专为从事 web 3 的公司、 DAO 和投资者设计。在 Loop ,Loop Crypto相信一个去中心化、可互操作的未来,并专注于加密交易的自动化以加速未来的发展。Loop Crypto允许用户在手动任务上花费更少的时间,而将更多的时间用于构建 web 3 。
# Help with MySQL and Python
Your first step is to pick out a table design. Figure out how you want to store the data. Write it down and make sure you know what type of data each thing you’re storing in the database is. Remember, for each record, you need a primary key.
Now, you’re going to run some code. Open command line, and go to your Blackjack folder (changing directory) in the command line. Now, change directory (CD) to the flask/Scripts directory.
Type `activate`. This activates the python virtual environment. You’ll see the characters `(venv)` appear to the left before your current directory in command line if this worked correctly.
Now, in the same manner as we wrote code for SQLite, we can create a cursor and run MySQL commands with MySQL. Go ahead and import the MySQL Connector in the file you want to run a SQL statement in:
```
import mysql.connector #imports the MySQL connector.
```
Then, you can create a cursor based on your MySQL username, host, and the database you're working with. Ask for the database from the database administrator. Keep in mind: you can have more than one table in one database, so you shouldn't need more than one database.
```
cnx = mysql.connector.connect(user=###, password=###, host =###, database=###) #makes the connection to the database
cur = cnx.cursor() #gets a cursor
```
Now, you can run MySQL commands! Reference your book for these, or this website.[http://cse.unl.edu/~sscott/ShowFiles/SQL/CheatSheet/SQLCheatSheet.html](http://cse.unl.edu/~sscott/ShowFiles/SQL/CheatSheet/SQLCheatSheet.html)
The way you run a MySQL command is by wrapping it in a cur.execute command. So if you wanted to create a new table, you would run:
cur.execute('CREATE TABLE IF NOT EXISTS testing ( LastName varchar(30) NOT NULL, FirstName varchar(30) NOT NULL, StudentID int NOT NULL, Major varchar(20), Dorm varchar(20), PRIMARY KEY (StudentID))')
[SQL Example from: [http://cse.unl.edu/~sscott/ShowFiles/SQL/CheatSheet/SQLCheatSheet.html](http://cse.unl.edu/~sscott/ShowFiles/SQL/CheatSheet/SQLCheatSheet.html)]
This would create a new table called `testing` if it doesn't already exist. This means this code can be run repeatedly (i.e. every time you started the website) without an issue. This new table would store a Last Name (max. 30 chars), a first name (max 30 chars), and a StudentID (an int) that had to be filled in. It also would store a major, and a Dorm, 20 chars each, that are optional as entries. It then notes at the end that the Primary Key is the StudentID. The table is now created!
To now add one row to this database, you would write:
cur.execute('INSERT INTO testing VALUES ('Smith','John',123456789,'Math','Selleck')')
This would add a John Smith name with ID 123456789 to the database. Note that you don’t set a dorm, but it’s ok because it’s optional!
You can also add this record like this:
cur.execute('INSERT INTO testing SET FirstName='John', LastName='Smith', StudentID=123456789, Major='Math'’)
This would do the same thing.
Now, we want to make sure this was added. So you can run:
cur.execute('select * from testing')
for r in cur:
print(r)
This will print, to the command line, the one record you added to the database.
Finally, close your connection with:
'cnx.close'
Once you’ve set up your database and played around with this, you can move on to using your database on the website.
To do this, you’ll run commands to create your database in your __init.py__ file, but you’ll be saving data from other parts of the website, so your database code for saving/retrieving data will go there. For example, say you want to save a username to the database...
You’ll need to open a connection and get a cursor:
import mysql.connector
cnx = mysql.connector.connect(user='###', password='###', host = '###', database='###')
cur = cnx.cursor()
And then find where the data is being saved as a variable in the website. You can run a MySQL command using cur.execute to insert that variable into the database, same as we tested out inserting that record from the code above. I would make the string command first. Something like…
query = 'INSERT INTO login SET username=‘+ form.name.data +’ time=‘+ time +’'
Then run `cur.execute(query)` to carry this information out. Please note that just saying time won’t work, of course, but you also know how to use the date time library from your previous Python bookwork!
Let me know if you have any questions!