├── Python-MySql DB.ipynb ├── Python-MySql DB.py ├── connect.py ├── create_db.py ├── create_table.py ├── curds_app.py ├── delete.py ├── insert_many.py ├── insert_one.py ├── select.py ├── select_one.py └── update.py /Python-MySql DB.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Connect To Database Server" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [ 15 | { 16 | "name": "stdout", 17 | "output_type": "stream", 18 | "text": [ 19 | "Database Connected\n" 20 | ] 21 | } 22 | ], 23 | "source": [ 24 | "import mysql.connector\n", 25 | "\n", 26 | "db = mysql.connector.connect(\n", 27 | " host=\"127.0.0.1\",\n", 28 | " user=\"root\",\n", 29 | " password=\"123456\"\n", 30 | ")\n", 31 | "\n", 32 | "if db.is_connected():\n", 33 | " print(\"Database Connected\")" 34 | ] 35 | }, 36 | { 37 | "cell_type": "markdown", 38 | "metadata": {}, 39 | "source": [ 40 | "### Create Database" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 1, 46 | "metadata": {}, 47 | "outputs": [ 48 | { 49 | "name": "stdout", 50 | "output_type": "stream", 51 | "text": [ 52 | "Database Created Successful !!!\n" 53 | ] 54 | } 55 | ], 56 | "source": [ 57 | "import mysql.connector\n", 58 | "\n", 59 | "db = mysql.connector.connect(\n", 60 | " host=\"127.0.0.1\",\n", 61 | " user=\"root\",\n", 62 | " password=\"123456\"\n", 63 | ")\n", 64 | "\n", 65 | "cursor = db.cursor()\n", 66 | "cursor.execute(\"CREATE DATABASE employee_data\")\n", 67 | "\n", 68 | "print(\"Database Created Successful !!!\")" 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "metadata": {}, 74 | "source": [ 75 | "### Create Table" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": 4, 81 | "metadata": {}, 82 | "outputs": [ 83 | { 84 | "name": "stdout", 85 | "output_type": "stream", 86 | "text": [ 87 | "Table Created Successful !!!\n" 88 | ] 89 | } 90 | ], 91 | "source": [ 92 | "import mysql.connector\n", 93 | "\n", 94 | "db = mysql.connector.connect(\n", 95 | " host=\"127.0.0.1\",\n", 96 | " user=\"root\",\n", 97 | " password=\"123456\",\n", 98 | " database=\"employee_data\",\n", 99 | ")\n", 100 | "\n", 101 | "cursor = db.cursor()\n", 102 | "sql = \"\"\"CREATE TABLE customers (\n", 103 | " customer_id INT AUTO_INCREMENT PRIMARY KEY,\n", 104 | " name VARCHAR(255),\n", 105 | " address Varchar(255)\n", 106 | ")\n", 107 | "\"\"\"\n", 108 | "cursor.execute(sql)\n", 109 | "\n", 110 | "print(\"Table Created Successful !!!\")" 111 | ] 112 | }, 113 | { 114 | "cell_type": "markdown", 115 | "metadata": {}, 116 | "source": [ 117 | "### Insert One Data " 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": 1, 123 | "metadata": {}, 124 | "outputs": [ 125 | { 126 | "name": "stdout", 127 | "output_type": "stream", 128 | "text": [ 129 | "1 data added\n" 130 | ] 131 | } 132 | ], 133 | "source": [ 134 | "import mysql.connector\n", 135 | "\n", 136 | "db = mysql.connector.connect(\n", 137 | " host=\"127.0.0.1\",\n", 138 | " user=\"root\",\n", 139 | " password=\"123456\",\n", 140 | " database=\"employee_data\",\n", 141 | ")\n", 142 | "\n", 143 | "cursor = db.cursor()\n", 144 | "sql = \"INSERT INTO customers (name, address) VALUES (%s, %s)\"\n", 145 | "val = (\"Mr Taif\", \"Dhaka\")\n", 146 | "cursor.execute(sql, val)\n", 147 | "\n", 148 | "db.commit()\n", 149 | "\n", 150 | "print(\"{} data added\".format(cursor.rowcount))" 151 | ] 152 | }, 153 | { 154 | "cell_type": "markdown", 155 | "metadata": {}, 156 | "source": [ 157 | "### Insert Many Data" 158 | ] 159 | }, 160 | { 161 | "cell_type": "code", 162 | "execution_count": 3, 163 | "metadata": {}, 164 | "outputs": [ 165 | { 166 | "name": "stdout", 167 | "output_type": "stream", 168 | "text": [ 169 | "1 data added\n" 170 | ] 171 | } 172 | ], 173 | "source": [ 174 | "import mysql.connector\n", 175 | "\n", 176 | "db = mysql.connector.connect(\n", 177 | " host=\"127.0.0.1\",\n", 178 | " user=\"root\",\n", 179 | " password=\"123456\",\n", 180 | " database=\"employee_data\",\n", 181 | ")\n", 182 | "\n", 183 | "cursor = db.cursor()\n", 184 | "sql = \"INSERT INTO customers (name, address) VALUES (%s, %s)\"\n", 185 | "values = [\n", 186 | " (\"Shakib\", \"Magura\"),\n", 187 | " (\"Tamin\", \"Ctg\"),\n", 188 | " (\"Taskin\", \"Dhaka\"),\n", 189 | " (\"Mushfiq\", \"Rajshahi\")\n", 190 | "]\n", 191 | "\n", 192 | "for val in values:\n", 193 | " cursor.execute(sql, val)\n", 194 | "\n", 195 | "db.commit()\n", 196 | "\n", 197 | "print(\"{} data added\".format(cursor.rowcount))" 198 | ] 199 | }, 200 | { 201 | "cell_type": "markdown", 202 | "metadata": {}, 203 | "source": [ 204 | "### Select " 205 | ] 206 | }, 207 | { 208 | "cell_type": "code", 209 | "execution_count": 6, 210 | "metadata": {}, 211 | "outputs": [ 212 | { 213 | "name": "stdout", 214 | "output_type": "stream", 215 | "text": [ 216 | "(1, 'Mr Taif', 'Dhaka')\n" 217 | ] 218 | } 219 | ], 220 | "source": [ 221 | "import mysql.connector\n", 222 | "\n", 223 | "db = mysql.connector.connect(\n", 224 | " host=\"127.0.0.1\",\n", 225 | " user=\"root\",\n", 226 | " password=\"123456\",\n", 227 | " database=\"employee_data\",\n", 228 | ")\n", 229 | "\n", 230 | "cursor = db.cursor()\n", 231 | "sql = \"SELECT * FROM customers\"\n", 232 | "cursor.execute(sql)\n", 233 | "\n", 234 | "result = cursor.fetchone()\n", 235 | "\n", 236 | "print(result)" 237 | ] 238 | }, 239 | { 240 | "cell_type": "markdown", 241 | "metadata": {}, 242 | "source": [ 243 | "### Fetch All Data" 244 | ] 245 | }, 246 | { 247 | "cell_type": "code", 248 | "execution_count": 8, 249 | "metadata": {}, 250 | "outputs": [ 251 | { 252 | "name": "stdout", 253 | "output_type": "stream", 254 | "text": [ 255 | "(1, 'Mr Taif', 'Dhaka')\n", 256 | "(2, 'Shakib', 'Magura')\n", 257 | "(3, 'Tamin', 'Ctg')\n", 258 | "(4, 'Taskin', 'Dhaka')\n", 259 | "(5, 'Mushfiq', 'Rajshahi')\n", 260 | "(6, 'Shakib', 'Magura')\n", 261 | "(7, 'Tamin', 'Ctg')\n", 262 | "(8, 'Taskin', 'Dhaka')\n", 263 | "(9, 'Mushfiq', 'Rajshahi')\n" 264 | ] 265 | } 266 | ], 267 | "source": [ 268 | "import mysql.connector\n", 269 | "\n", 270 | "db = mysql.connector.connect(\n", 271 | " host=\"127.0.0.1\",\n", 272 | " user=\"root\",\n", 273 | " password=\"123456\",\n", 274 | " database=\"employee_data\",\n", 275 | ")\n", 276 | "\n", 277 | "cursor = db.cursor()\n", 278 | "sql = \"SELECT * FROM customers\"\n", 279 | "cursor.execute(sql)\n", 280 | "\n", 281 | "results = cursor.fetchall()\n", 282 | "\n", 283 | "for data in results:\n", 284 | " print(data)" 285 | ] 286 | }, 287 | { 288 | "cell_type": "markdown", 289 | "metadata": {}, 290 | "source": [ 291 | "### Delete Data" 292 | ] 293 | }, 294 | { 295 | "cell_type": "code", 296 | "execution_count": 13, 297 | "metadata": {}, 298 | "outputs": [ 299 | { 300 | "name": "stdout", 301 | "output_type": "stream", 302 | "text": [ 303 | "1 data deleted\n" 304 | ] 305 | } 306 | ], 307 | "source": [ 308 | "import mysql.connector\n", 309 | "\n", 310 | "db = mysql.connector.connect(\n", 311 | " host=\"127.0.0.1\",\n", 312 | " user=\"root\",\n", 313 | " password=\"123456\",\n", 314 | " database=\"employee_data\",\n", 315 | ")\n", 316 | "\n", 317 | "cursor = db.cursor()\n", 318 | "sql = \"DELETE FROM customers WHERE customer_id=%s\"\n", 319 | "val = (4, )\n", 320 | "cursor.execute(sql, val)\n", 321 | "\n", 322 | "db.commit()\n", 323 | "\n", 324 | "print(\"{} data deleted\".format(cursor.rowcount))" 325 | ] 326 | }, 327 | { 328 | "cell_type": "markdown", 329 | "metadata": {}, 330 | "source": [ 331 | "### Update Data" 332 | ] 333 | }, 334 | { 335 | "cell_type": "code", 336 | "execution_count": 14, 337 | "metadata": {}, 338 | "outputs": [ 339 | { 340 | "name": "stdout", 341 | "output_type": "stream", 342 | "text": [ 343 | "1 data changed\n" 344 | ] 345 | } 346 | ], 347 | "source": [ 348 | "import mysql.connector\n", 349 | "\n", 350 | "db = mysql.connector.connect(\n", 351 | " host=\"127.0.0.1\",\n", 352 | " user=\"root\",\n", 353 | " password=\"123456\",\n", 354 | " database=\"employee_data\",\n", 355 | ")\n", 356 | "\n", 357 | "cursor = db.cursor()\n", 358 | "sql = \"UPDATE customers SET name=%s, address=%s WHERE customer_id=%s\"\n", 359 | "val = (\"ShakibAL\", \"Dhaka\", 2)\n", 360 | "cursor.execute(sql, val)\n", 361 | "\n", 362 | "db.commit()\n", 363 | "\n", 364 | "print(\"{} data changed\".format(cursor.rowcount))" 365 | ] 366 | }, 367 | { 368 | "cell_type": "markdown", 369 | "metadata": {}, 370 | "source": [ 371 | "### curds_app" 372 | ] 373 | }, 374 | { 375 | "cell_type": "code", 376 | "execution_count": null, 377 | "metadata": {}, 378 | "outputs": [ 379 | { 380 | "name": "stdout", 381 | "output_type": "stream", 382 | "text": [ 383 | "=== APPLICATION DATABASE PYTHON ===\n", 384 | "1. Insert Data\n", 385 | "2. Show Data\n", 386 | "3. Update Data\n", 387 | "4. Delete Data\n", 388 | "5. Search Data\n", 389 | "0. GO Out\n", 390 | "------------------\n", 391 | "Choose menu> 2\n", 392 | "(2, 'ShakibAL', 'Dhaka')\n", 393 | "(5, 'Mushfiq', 'Rajshahi')\n", 394 | "(6, 'Shakib', 'Magura')\n", 395 | "(7, 'Tamin', 'Ctg')\n", 396 | "(9, 'Mushfiq', 'Rajshahi')\n", 397 | "=== APPLICATION DATABASE PYTHON ===\n", 398 | "1. Insert Data\n", 399 | "2. Show Data\n", 400 | "3. Update Data\n", 401 | "4. Delete Data\n", 402 | "5. Search Data\n", 403 | "0. GO Out\n", 404 | "------------------\n", 405 | "Choose menu> 1\n", 406 | "Enter Name: 2\n", 407 | "Enter Address: Dhaka\n", 408 | "1 data Inserted\n", 409 | "=== APPLICATION DATABASE PYTHON ===\n", 410 | "1. Insert Data\n", 411 | "2. Show Data\n", 412 | "3. Update Data\n", 413 | "4. Delete Data\n", 414 | "5. Search Data\n", 415 | "0. GO Out\n", 416 | "------------------\n", 417 | "Choose menu> 2\n", 418 | "(2, 'ShakibAL', 'Dhaka')\n", 419 | "(5, 'Mushfiq', 'Rajshahi')\n", 420 | "(6, 'Shakib', 'Magura')\n", 421 | "(7, 'Tamin', 'Ctg')\n", 422 | "(9, 'Mushfiq', 'Rajshahi')\n", 423 | "(10, '2', 'Dhaka')\n", 424 | "=== APPLICATION DATABASE PYTHON ===\n", 425 | "1. Insert Data\n", 426 | "2. Show Data\n", 427 | "3. Update Data\n", 428 | "4. Delete Data\n", 429 | "5. Search Data\n", 430 | "0. GO Out\n", 431 | "------------------\n", 432 | "Choose menu> 0\n", 433 | "=== APPLICATION DATABASE PYTHON ===\n", 434 | "1. Insert Data\n", 435 | "2. Show Data\n", 436 | "3. Update Data\n", 437 | "4. Delete Data\n", 438 | "5. Search Data\n", 439 | "0. GO Out\n", 440 | "------------------\n" 441 | ] 442 | } 443 | ], 444 | "source": [ 445 | "import mysql.connector\n", 446 | "import os\n", 447 | "\n", 448 | "db = mysql.connector.connect(\n", 449 | " host=\"127.0.0.1\",\n", 450 | " user=\"root\",\n", 451 | " password=\"123456\",\n", 452 | " database=\"employee_data\",\n", 453 | ")\n", 454 | "\n", 455 | "\n", 456 | "def insert_data(db):\n", 457 | " name = input(\"Enter Name: \")\n", 458 | " address = input(\"Enter Address: \")\n", 459 | " val = (name, address)\n", 460 | " cursor = db.cursor()\n", 461 | " sql = \"INSERT INTO customers (name, address) VALUES (%s, %s)\"\n", 462 | " cursor.execute(sql, val)\n", 463 | " db.commit()\n", 464 | " print(\"{} data Inserted\".format(cursor.rowcount))\n", 465 | "\n", 466 | "\n", 467 | "def show_data(db):\n", 468 | " cursor = db.cursor()\n", 469 | " sql = \"SELECT * FROM customers\"\n", 470 | " cursor.execute(sql)\n", 471 | " results = cursor.fetchall()\n", 472 | " \n", 473 | " if cursor.rowcount < 0:\n", 474 | " print(\"There is not any data\")\n", 475 | " else:\n", 476 | " for data in results:\n", 477 | " print(data)\n", 478 | "\n", 479 | "\n", 480 | "def update_data(db):\n", 481 | " cursor = db.cursor()\n", 482 | " show_data(db)\n", 483 | " customer_id = input(\"Choose id customer> \")\n", 484 | " name = input(\"New Name: \")\n", 485 | " address = input(\"New Address: \")\n", 486 | "\n", 487 | " sql = \"UPDATE customers SET name=%s, address=%s WHERE customer_id=%s\"\n", 488 | " val = (name, address, customer_id)\n", 489 | " cursor.execute(sql, val)\n", 490 | " db.commit()\n", 491 | " print(\"{} data successfully changed\".format(cursor.rowcount))\n", 492 | "\n", 493 | "\n", 494 | "def delete_data(db):\n", 495 | " cursor = db.cursor()\n", 496 | " show_data(db)\n", 497 | " customer_id = input(\"Choose id customer> \")\n", 498 | " sql = \"DELETE FROM customers WHERE customer_id=%s\"\n", 499 | " val = (customer_id,)\n", 500 | " cursor.execute(sql, val)\n", 501 | " db.commit()\n", 502 | " print(\"{} data successfully deleted\".format(cursor.rowcount))\n", 503 | "\n", 504 | "\n", 505 | "def search_data(db):\n", 506 | " cursor = db.cursor()\n", 507 | " keyword = input(\"Keyword: \")\n", 508 | " sql = \"SELECT * FROM customers WHERE name LIKE %s OR address LIKE %s\"\n", 509 | " val = (\"%{}%\".format(keyword), \"%{}%\".format(keyword))\n", 510 | " cursor.execute(sql, val)\n", 511 | " results = cursor.fetchall()\n", 512 | " \n", 513 | " if cursor.rowcount < 0:\n", 514 | " print(\"There is not any data\")\n", 515 | " else:\n", 516 | " for data in results:\n", 517 | " print(data)\n", 518 | "\n", 519 | "\n", 520 | "def show_menu(db):\n", 521 | " print(\"=== APPLICATION DATABASE PYTHON ===\")\n", 522 | " print(\"1. Insert Data\")\n", 523 | " print(\"2. Show Data\")\n", 524 | " print(\"3. Update Data\")\n", 525 | " print(\"4. Delete Data\")\n", 526 | " print(\"5. Search Data\")\n", 527 | " print(\"0. GO Out\")\n", 528 | " print(\"------------------\")\n", 529 | " menu = input(\"Choose menu> \")\n", 530 | "\n", 531 | " #clear screen\n", 532 | " os.system(\"clear\")\n", 533 | "\n", 534 | " if menu == \"1\":\n", 535 | " insert_data(db)\n", 536 | " elif menu == \"2\":\n", 537 | " show_data(db)\n", 538 | " elif menu == \"3\":\n", 539 | " update_data(db)\n", 540 | " elif menu == \"4\":\n", 541 | " delete_data(db)\n", 542 | " elif menu == \"5\":\n", 543 | " search_data(db)\n", 544 | " elif menu == \"0\":\n", 545 | " exit()\n", 546 | " else:\n", 547 | " print(\"Menu WRONG!\")\n", 548 | "\n", 549 | "\n", 550 | "if __name__ == \"__main__\":\n", 551 | " while(True):\n", 552 | " show_menu(db)" 553 | ] 554 | }, 555 | { 556 | "cell_type": "code", 557 | "execution_count": null, 558 | "metadata": {}, 559 | "outputs": [], 560 | "source": [] 561 | } 562 | ], 563 | "metadata": { 564 | "kernelspec": { 565 | "display_name": "Python 3", 566 | "language": "python", 567 | "name": "python3" 568 | }, 569 | "language_info": { 570 | "codemirror_mode": { 571 | "name": "ipython", 572 | "version": 3 573 | }, 574 | "file_extension": ".py", 575 | "mimetype": "text/x-python", 576 | "name": "python", 577 | "nbconvert_exporter": "python", 578 | "pygments_lexer": "ipython3", 579 | "version": "3.7.4" 580 | } 581 | }, 582 | "nbformat": 4, 583 | "nbformat_minor": 2 584 | } 585 | -------------------------------------------------------------------------------- /Python-MySql DB.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # ### Connect To Database Server 5 | 6 | # In[1]: 7 | 8 | 9 | import mysql.connector 10 | 11 | db = mysql.connector.connect( 12 | host="127.0.0.1", 13 | user="root", 14 | password="123456" 15 | ) 16 | 17 | if db.is_connected(): 18 | print("Database Connected") 19 | 20 | 21 | # ### Create Database 22 | 23 | # In[1]: 24 | 25 | 26 | import mysql.connector 27 | 28 | db = mysql.connector.connect( 29 | host="127.0.0.1", 30 | user="root", 31 | password="123456" 32 | ) 33 | 34 | cursor = db.cursor() 35 | cursor.execute("CREATE DATABASE employee_data") 36 | 37 | print("Database Created Successful !!!") 38 | 39 | 40 | # ### Create Table 41 | 42 | # In[4]: 43 | 44 | 45 | import mysql.connector 46 | 47 | db = mysql.connector.connect( 48 | host="127.0.0.1", 49 | user="root", 50 | password="123456", 51 | database="employee_data", 52 | ) 53 | 54 | cursor = db.cursor() 55 | sql = """CREATE TABLE customers ( 56 | customer_id INT AUTO_INCREMENT PRIMARY KEY, 57 | name VARCHAR(255), 58 | address Varchar(255) 59 | ) 60 | """ 61 | cursor.execute(sql) 62 | 63 | print("Table Created Successful !!!") 64 | 65 | 66 | # ### Insert One Data 67 | 68 | # In[1]: 69 | 70 | 71 | import mysql.connector 72 | 73 | db = mysql.connector.connect( 74 | host="127.0.0.1", 75 | user="root", 76 | password="123456", 77 | database="employee_data", 78 | ) 79 | 80 | cursor = db.cursor() 81 | sql = "INSERT INTO customers (name, address) VALUES (%s, %s)" 82 | val = ("Mr Taif", "Dhaka") 83 | cursor.execute(sql, val) 84 | 85 | db.commit() 86 | 87 | print("{} data added".format(cursor.rowcount)) 88 | 89 | 90 | # ### Insert Many Data 91 | 92 | # In[3]: 93 | 94 | 95 | import mysql.connector 96 | 97 | db = mysql.connector.connect( 98 | host="127.0.0.1", 99 | user="root", 100 | password="123456", 101 | database="employee_data", 102 | ) 103 | 104 | cursor = db.cursor() 105 | sql = "INSERT INTO customers (name, address) VALUES (%s, %s)" 106 | values = [ 107 | ("Shakib", "Magura"), 108 | ("Tamin", "Ctg"), 109 | ("Taskin", "Dhaka"), 110 | ("Mushfiq", "Rajshahi") 111 | ] 112 | 113 | for val in values: 114 | cursor.execute(sql, val) 115 | 116 | db.commit() 117 | 118 | print("{} data added".format(cursor.rowcount)) 119 | 120 | 121 | # ### Select 122 | 123 | # In[6]: 124 | 125 | 126 | import mysql.connector 127 | 128 | db = mysql.connector.connect( 129 | host="127.0.0.1", 130 | user="root", 131 | password="123456", 132 | database="employee_data", 133 | ) 134 | 135 | cursor = db.cursor() 136 | sql = "SELECT * FROM customers" 137 | cursor.execute(sql) 138 | 139 | result = cursor.fetchone() 140 | 141 | print(result) 142 | 143 | 144 | # ### Fetch All Data 145 | 146 | # In[8]: 147 | 148 | 149 | import mysql.connector 150 | 151 | db = mysql.connector.connect( 152 | host="127.0.0.1", 153 | user="root", 154 | password="123456", 155 | database="employee_data", 156 | ) 157 | 158 | cursor = db.cursor() 159 | sql = "SELECT * FROM customers" 160 | cursor.execute(sql) 161 | 162 | results = cursor.fetchall() 163 | 164 | for data in results: 165 | print(data) 166 | 167 | 168 | # ### Delete Data 169 | 170 | # In[13]: 171 | 172 | 173 | import mysql.connector 174 | 175 | db = mysql.connector.connect( 176 | host="127.0.0.1", 177 | user="root", 178 | password="123456", 179 | database="employee_data", 180 | ) 181 | 182 | cursor = db.cursor() 183 | sql = "DELETE FROM customers WHERE customer_id=%s" 184 | val = (4, ) 185 | cursor.execute(sql, val) 186 | 187 | db.commit() 188 | 189 | print("{} data deleted".format(cursor.rowcount)) 190 | 191 | 192 | # ### Update Data 193 | 194 | # In[14]: 195 | 196 | 197 | import mysql.connector 198 | 199 | db = mysql.connector.connect( 200 | host="127.0.0.1", 201 | user="root", 202 | password="123456", 203 | database="employee_data", 204 | ) 205 | 206 | cursor = db.cursor() 207 | sql = "UPDATE customers SET name=%s, address=%s WHERE customer_id=%s" 208 | val = ("ShakibAL", "Dhaka", 2) 209 | cursor.execute(sql, val) 210 | 211 | db.commit() 212 | 213 | print("{} data changed".format(cursor.rowcount)) 214 | 215 | 216 | # ### curds_app 217 | 218 | # In[ ]: 219 | 220 | 221 | import mysql.connector 222 | import os 223 | 224 | db = mysql.connector.connect( 225 | host="127.0.0.1", 226 | user="root", 227 | password="123456", 228 | database="employee_data", 229 | ) 230 | 231 | 232 | def insert_data(db): 233 | name = input("Enter Name: ") 234 | address = input("Enter Address: ") 235 | val = (name, address) 236 | cursor = db.cursor() 237 | sql = "INSERT INTO customers (name, address) VALUES (%s, %s)" 238 | cursor.execute(sql, val) 239 | db.commit() 240 | print("{} data Inserted".format(cursor.rowcount)) 241 | 242 | 243 | def show_data(db): 244 | cursor = db.cursor() 245 | sql = "SELECT * FROM customers" 246 | cursor.execute(sql) 247 | results = cursor.fetchall() 248 | 249 | if cursor.rowcount < 0: 250 | print("There is not any data") 251 | else: 252 | for data in results: 253 | print(data) 254 | 255 | 256 | def update_data(db): 257 | cursor = db.cursor() 258 | show_data(db) 259 | customer_id = input("Choose id customer> ") 260 | name = input("New Name: ") 261 | address = input("New Address: ") 262 | 263 | sql = "UPDATE customers SET name=%s, address=%s WHERE customer_id=%s" 264 | val = (name, address, customer_id) 265 | cursor.execute(sql, val) 266 | db.commit() 267 | print("{} data successfully changed".format(cursor.rowcount)) 268 | 269 | 270 | def delete_data(db): 271 | cursor = db.cursor() 272 | show_data(db) 273 | customer_id = input("Choose id customer> ") 274 | sql = "DELETE FROM customers WHERE customer_id=%s" 275 | val = (customer_id,) 276 | cursor.execute(sql, val) 277 | db.commit() 278 | print("{} data successfully deleted".format(cursor.rowcount)) 279 | 280 | 281 | def search_data(db): 282 | cursor = db.cursor() 283 | keyword = input("Keyword: ") 284 | sql = "SELECT * FROM customers WHERE name LIKE %s OR address LIKE %s" 285 | val = ("%{}%".format(keyword), "%{}%".format(keyword)) 286 | cursor.execute(sql, val) 287 | results = cursor.fetchall() 288 | 289 | if cursor.rowcount < 0: 290 | print("There is not any data") 291 | else: 292 | for data in results: 293 | print(data) 294 | 295 | 296 | def show_menu(db): 297 | print("=== APPLICATION DATABASE PYTHON ===") 298 | print("1. Insert Data") 299 | print("2. Show Data") 300 | print("3. Update Data") 301 | print("4. Delete Data") 302 | print("5. Search Data") 303 | print("0. GO Out") 304 | print("------------------") 305 | menu = input("Choose menu> ") 306 | 307 | #clear screen 308 | os.system("clear") 309 | 310 | if menu == "1": 311 | insert_data(db) 312 | elif menu == "2": 313 | show_data(db) 314 | elif menu == "3": 315 | update_data(db) 316 | elif menu == "4": 317 | delete_data(db) 318 | elif menu == "5": 319 | search_data(db) 320 | elif menu == "0": 321 | exit() 322 | else: 323 | print("Menu WRONG!") 324 | 325 | 326 | if __name__ == "__main__": 327 | while(True): 328 | show_menu(db) 329 | 330 | 331 | # In[ ]: 332 | 333 | 334 | 335 | 336 | -------------------------------------------------------------------------------- /connect.py: -------------------------------------------------------------------------------- 1 | import mysql.connector 2 | 3 | db = mysql.connector.connect( 4 | host="127.0.0.1", 5 | user="root", 6 | password="123456" 7 | ) 8 | 9 | if db.is_connected(): 10 | print("Database Connected") -------------------------------------------------------------------------------- /create_db.py: -------------------------------------------------------------------------------- 1 | import mysql.connector 2 | 3 | db = mysql.connector.connect( 4 | host="127.0.0.1", 5 | user="root", 6 | password="123456" 7 | ) 8 | 9 | cursor = db.cursor() 10 | cursor.execute("CREATE DATABASE employee_data") 11 | 12 | print("Database Created Successful !!!") -------------------------------------------------------------------------------- /create_table.py: -------------------------------------------------------------------------------- 1 | import mysql.connector 2 | 3 | db = mysql.connector.connect( 4 | host="127.0.0.1", 5 | user="root", 6 | password="123456", 7 | database="employee_data", 8 | ) 9 | 10 | cursor = db.cursor() 11 | sql = """CREATE TABLE customers ( 12 | customer_id INT AUTO_INCREMENT PRIMARY KEY, 13 | name VARCHAR(255), 14 | address Varchar(255) 15 | ) 16 | """ 17 | cursor.execute(sql) 18 | 19 | print("Table Created Successful !!!") -------------------------------------------------------------------------------- /curds_app.py: -------------------------------------------------------------------------------- 1 | import mysql.connector 2 | import os 3 | 4 | db = mysql.connector.connect( 5 | host="127.0.0.1", 6 | user="root", 7 | password="123456", 8 | database="employee_data", 9 | ) 10 | 11 | 12 | def insert_data(db): 13 | name = input("Enter Name: ") 14 | address = input("Enter Address: ") 15 | val = (name, address) 16 | cursor = db.cursor() 17 | sql = "INSERT INTO customers (name, address) VALUES (%s, %s)" 18 | cursor.execute(sql, val) 19 | db.commit() 20 | print("{} data Inserted".format(cursor.rowcount)) 21 | 22 | 23 | def show_data(db): 24 | cursor = db.cursor() 25 | sql = "SELECT * FROM customers" 26 | cursor.execute(sql) 27 | results = cursor.fetchall() 28 | 29 | if cursor.rowcount < 0: 30 | print("There is not any data") 31 | else: 32 | for data in results: 33 | print(data) 34 | 35 | 36 | def update_data(db): 37 | cursor = db.cursor() 38 | show_data(db) 39 | customer_id = input("Choose id customer> ") 40 | name = input("New Name: ") 41 | address = input("New Address: ") 42 | 43 | sql = "UPDATE customers SET name=%s, address=%s WHERE customer_id=%s" 44 | val = (name, address, customer_id) 45 | cursor.execute(sql, val) 46 | db.commit() 47 | print("{} data successfully changed".format(cursor.rowcount)) 48 | 49 | 50 | def delete_data(db): 51 | cursor = db.cursor() 52 | show_data(db) 53 | customer_id = input("Choose id customer> ") 54 | sql = "DELETE FROM customers WHERE customer_id=%s" 55 | val = (customer_id,) 56 | cursor.execute(sql, val) 57 | db.commit() 58 | print("{} data successfully deleted".format(cursor.rowcount)) 59 | 60 | 61 | def search_data(db): 62 | cursor = db.cursor() 63 | keyword = input("Keyword: ") 64 | sql = "SELECT * FROM customers WHERE name LIKE %s OR address LIKE %s" 65 | val = ("%{}%".format(keyword), "%{}%".format(keyword)) 66 | cursor.execute(sql, val) 67 | results = cursor.fetchall() 68 | 69 | if cursor.rowcount < 0: 70 | print("There is not any data") 71 | else: 72 | for data in results: 73 | print(data) 74 | 75 | 76 | def show_menu(db): 77 | print("=== APPLICATION DATABASE PYTHON ===") 78 | print("1. Insert Data") 79 | print("2. Show Data") 80 | print("3. Update Data") 81 | print("4. Delete Data") 82 | print("5. Search Data") 83 | print("0. GO Out") 84 | print("------------------") 85 | menu = input("Choose menu> ") 86 | 87 | #clear screen 88 | os.system("clear") 89 | 90 | if menu == "1": 91 | insert_data(db) 92 | elif menu == "2": 93 | show_data(db) 94 | elif menu == "3": 95 | update_data(db) 96 | elif menu == "4": 97 | delete_data(db) 98 | elif menu == "5": 99 | search_data(db) 100 | elif menu == "0": 101 | exit() 102 | else: 103 | print("Menu WRONG!") 104 | 105 | 106 | if __name__ == "__main__": 107 | while(True): 108 | show_menu(db) -------------------------------------------------------------------------------- /delete.py: -------------------------------------------------------------------------------- 1 | import mysql.connector 2 | 3 | db = mysql.connector.connect( 4 | host="127.0.0.1", 5 | user="root", 6 | password="123456", 7 | database="employee_data", 8 | ) 9 | 10 | cursor = db.cursor() 11 | sql = "DELETE FROM customers WHERE customer_id=%s" 12 | val = (4, ) 13 | cursor.execute(sql, val) 14 | 15 | db.commit() 16 | 17 | print("{} data deleted".format(cursor.rowcount)) -------------------------------------------------------------------------------- /insert_many.py: -------------------------------------------------------------------------------- 1 | import mysql.connector 2 | 3 | db = mysql.connector.connect( 4 | host="127.0.0.1", 5 | user="root", 6 | password="123456", 7 | database="employee_data", 8 | ) 9 | 10 | cursor = db.cursor() 11 | sql = "INSERT INTO customers (name, address) VALUES (%s, %s)" 12 | values = [ 13 | ("Shakib", "Magura"), 14 | ("Tamin", "Ctg"), 15 | ("Taskin", "Dhaka"), 16 | ("Mushfiq", "Rajshahi") 17 | ] 18 | 19 | for val in values: 20 | cursor.execute(sql, val) 21 | 22 | db.commit() 23 | 24 | print("{} data added".format(cursor.rowcount)) -------------------------------------------------------------------------------- /insert_one.py: -------------------------------------------------------------------------------- 1 | import mysql.connector 2 | 3 | db = mysql.connector.connect( 4 | host="127.0.0.1", 5 | user="root", 6 | password="123456", 7 | database="employee_data", 8 | ) 9 | 10 | cursor = db.cursor() 11 | sql = "INSERT INTO customers (name, address) VALUES (%s, %s)" 12 | val = ("Mr Taif", "Dhaka") 13 | cursor.execute(sql, val) 14 | 15 | db.commit() 16 | 17 | print("{} data added".format(cursor.rowcount)) -------------------------------------------------------------------------------- /select.py: -------------------------------------------------------------------------------- 1 | import mysql.connector 2 | 3 | db = mysql.connector.connect( 4 | host="127.0.0.1", 5 | user="root", 6 | password="123456", 7 | database="employee_data", 8 | ) 9 | 10 | cursor = db.cursor() 11 | sql = "SELECT * FROM customers" 12 | cursor.execute(sql) 13 | 14 | results = cursor.fetchall() 15 | 16 | for data in results: 17 | print(data) -------------------------------------------------------------------------------- /select_one.py: -------------------------------------------------------------------------------- 1 | import mysql.connector 2 | 3 | db = mysql.connector.connect( 4 | host="127.0.0.1", 5 | user="root", 6 | password="123456", 7 | database="employee_data", 8 | ) 9 | 10 | cursor = db.cursor() 11 | sql = "SELECT * FROM customers" 12 | cursor.execute(sql) 13 | 14 | result = cursor.fetchone() 15 | 16 | print(result) -------------------------------------------------------------------------------- /update.py: -------------------------------------------------------------------------------- 1 | import mysql.connector 2 | 3 | db = mysql.connector.connect( 4 | host="127.0.0.1", 5 | user="root", 6 | password="123456", 7 | database="employee_data", 8 | ) 9 | 10 | cursor = db.cursor() 11 | sql = "UPDATE customers SET name=%s, address=%s WHERE customer_id=%s" 12 | val = ("ShakibAL", "Dhaka", 2) 13 | cursor.execute(sql, val) 14 | 15 | db.commit() 16 | 17 | print("{} data changed".format(cursor.rowcount)) --------------------------------------------------------------------------------