Created
July 29, 2022 13:26
-
-
Save pj8912/95e26ac3b8544ad3d01048fbb9e9c04d to your computer and use it in GitHub Desktop.
python basic mysql CRUD functionalities
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- phpMyAdmin SQL Dump | |
-- version 5.2.0 | |
-- https://www.phpmyadmin.net/ | |
-- | |
-- Host: localhost | |
-- Generation Time: Jul 29, 2022 at 03:26 PM | |
-- Server version: 10.4.24-MariaDB | |
-- PHP Version: 8.0.19 | |
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; | |
START TRANSACTION; | |
SET time_zone = "+00:00"; | |
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; | |
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; | |
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; | |
/*!40101 SET NAMES utf8mb4 */; | |
-- | |
-- Database: `db_test` | |
-- | |
-- -------------------------------------------------------- | |
-- | |
-- Table structure for table `test_queries` | |
-- | |
CREATE TABLE `test_queries` ( | |
`tid` int(11) NOT NULL, | |
`test_notes` text DEFAULT NULL, | |
`on_date` datetime DEFAULT current_timestamp() | |
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; | |
-- | |
-- Dumping data for table `test_queries` | |
-- | |
INSERT INTO `test_queries` (`tid`, `test_notes`, `on_date`) VALUES | |
(1, 'updatecomplete!!', '2022-07-13 13:34:35'), | |
(2, 'asiphdpihaspidh paihsdpiapsihd piah spidhpiahsdpi hapishdpiahsd hai', '2022-07-13 13:34:35'), | |
(7, 'working from python', '2022-07-29 18:33:12'); | |
-- | |
-- Indexes for dumped tables | |
-- | |
-- | |
-- Indexes for table `test_queries` | |
-- | |
ALTER TABLE `test_queries` | |
ADD PRIMARY KEY (`tid`); | |
-- | |
-- AUTO_INCREMENT for dumped tables | |
-- | |
-- | |
-- AUTO_INCREMENT for table `test_queries` | |
-- | |
ALTER TABLE `test_queries` | |
MODIFY `tid` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=9; | |
COMMIT; | |
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; | |
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; | |
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pymysql | |
connection = pymysql.connect(host="localhost", user="root", passwd="", database="db_test") | |
cursor = connection.cursor(pymysql.cursors.DictCursor) | |
#fetch data | |
sql = "SELECT * FROM test_queries" | |
cursor.execute(sql) | |
for row in cursor: | |
print(row['test_notes']) | |
print("-------") | |
#insert data | |
sql = "INSERT INTO test_queries(test_notes) VALUES(%s)" | |
cursor.execute(sql,('working from python test2')) | |
connection.commit() | |
print("here we go again") | |
print("") | |
def fetchData(): | |
sql = "SELECT * FROM test_queries ORDER BY on_date DESC" | |
cursor.execute(sql) | |
row_count = cursor.rowcount #rowcount | |
if row_count > 0: | |
for row in cursor: | |
print(row['test_notes']) | |
else: | |
print("no data uploaded yet!") | |
#row count | |
#print("row count:", cursor.rowcount) | |
#update | |
sql = "UPDATE test_queries SET test_notes='updatecomplete!!' WHERE tid = 1" | |
cursor.execute(sql) | |
connection.commit() | |
print("") | |
fetchData() | |
#delete | |
sql = "DELETE FROM test_queries WHERE tid = 8" | |
cursor.execute(sql) | |
connection.commit() | |
print("") | |
fetchData() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment