MySQL Connector/Python උපදෙස්: Python එක MySQL වෙත ආරක්ෂිතව සම්බන්ධ කිරීම

1. MySQL Connector/Python හැඳින්වීම

MySQL Connector/Python යනු Python වැඩසටහන් MySQL දත්ත ගබඩාවකට සම්බන්ධ වී දත්ත ගබඩා මෙහෙයුම් සිදු කිරීමට ඉඩ දෙන නිල පුස්තකාලයයි. එය දත්ත ගබඩාවට සම්බන්ධ වීම, විමසුම් ක්‍රියාත්මක කිරීම, දත්ත ලබා ගැනීම හෝ යාවත්කාලීන කිරීම වැනි මූලික කාර්යයන් සරල හා කාර්යක්ෂම ආකාරයෙන් කළ හැකි කරයි. MySQL Connector/Python හි ප්‍රධාන වාසිය වන්නේ එය MySQL සමඟ සෘජු ඒකාබද්ධතාවයක් ලබා දීමත්, DB‑API 2.0 (Python දත්ත ගබඩා API ප්‍රමිතිය) සමඟ අනුකූල වීමත් වන අතර, මෙය වෙනත් දත්ත ගබඩා පද්ධති සමඟ සමාන අතුරුමුහුණතක් ලබා දේ. එමඟින් Python සංවර්ධකයන්ට දත්ත ගබඩා සමඟ එකම ආකාරයෙන් වැඩ කිරීමට හැකියාව ලැබේ.

MySQL Connector/Python භාවිතා කිරීමේ හේතුව කුමක්ද?

MySQL Connector/Python භාවිතා කිරීමෙන් ඔබට SQL injection වළක්වා ආරක්ෂිත දත්ත ගබඩා මෙහෙයුම් සිදු කළ හැක. එමෙන්ම Python හි වස්තු‑ආශ්‍රිත (object‑oriented) වැඩසටහන් ලක්ෂණ භාවිතා කර දත්ත ගබඩා මෙහෙයුම් වඩා කාර්යක්ෂම හා සවිස්තරාත්මක ලෙස ක්‍රියාත්මක කළ හැක. තවද, සූදානම් ප්‍රකාශ (prepared statements) සහ escaping වැනි උසස් විශේෂාංග ලබා දීම නිසා කාර්ය සාධනය සහ ආරක්ෂාව දෙකම සඳහා මෙය ඉතා හොඳ තේරීමකි.

2. MySQL Connector/Python සකස් කිරීම

MySQL Connector/Python භාවිතා කිරීම ආරම්භ කිරීමට ප්‍රථම ඔබේ පරිසරය සූදානම් කරගත යුතුය. පහතදී ස්ථාපන පියවර සහ පරිසර සැකසුම් පිළිබඳ විස්තර කර ඇත.

ස්ථාපනය කරන ආකාරය

MySQL Connector/Python පහසුවෙන් pip (Python පැකේජ කළමනාකරණ මෙවලම) භාවිතා කර ස්ථාපනය කළ හැක. පහත කමාන්ඩ් එක ක්‍රියාත්මක කරන්න.

pip install mysql-connector-python

මෙම කමාන්ඩ් එක ක්‍රියාත්මක කිරීමෙන් MySQL Connector/Python නවතම අනුවාදය ස්ථාපනය වේ.

සංවර්ධන පරිසරය සැකසීම

MySQL Connector/Python සමඟ කාර්යක්ෂමව සංවර්ධනය කිරීම සඳහා ඒකාබද්ධ සංවර්ධන පරිසරයක් (IDE) භාවිතා කිරීම ඉතා ප්‍රයෝජනවත් වේ. උදාහරණයක් ලෙස PyCharm, VS Code වැනි IDE ගණනාවක් කේත සම්පූර්ණ කිරීම, දෝෂ නිරාකරණය වැනි විශේෂාංග ලබා දේ, එමඟින් නිෂ්පාදනශීලීත්වය වැඩි වේ. ඔබේ IDE සැකසුම් තුළ සුදුසු Python interpreter එක තෝරා, එය ස්ථාපනය කර ඇති MySQL Connector/Python පැකේජය භාවිතා කරන ලෙස සකස් කරන්න.

3. MySQL වෙත සම්බන්ධ වීම

ඊළඟට, MySQL Connector/Python භාවිතා කර MySQL දත්ත ගබඩාවකට සම්බන්ධ වීමේ පියවර ගැන සාකච්ඡා කරමු. පළමුව සම්බන්ධතාවයේ මූලික සංකල්පය සහ අවශ්‍ය පරාමිතීන් සැකසීම පිළිබඳ විස්තර කරමු.

සම්බන්ධතා පරාමිතීන් සැකසීම

MySQL දත්ත ගබඩාවකට සම්බන්ධ වීමට ඔබට පහත තොරතුරු අවශ්‍ය වේ.

  • host : දත්ත ගබඩා සේවාදායකයේ hostname හෝ IP ලිපිනය
  • user : දත්ත ගබඩා පරිශීලක නාමය
  • password : පරිශීලකගේ මුරපදය
  • database : සම්බන්ධ වීමට අවශ්‍ය දත්ත ගබඩාවේ නාමය

මෙම තොරතුරු භාවිතා කර MySQL Connector/Python හි connect ක්‍රියාවලිය කැඳවා දත්ත ගබඩාවට සම්බන්ධ විය හැක.

නියැදි කේත උදාහරණය

MySQL දත්ත ගබඩාවකට සම්බන්ධ වීමේ මූලික කේත උදාහරණයක් පහත දැක්වේ.

import mysql.connector

# Connect to the database
conn = mysql.connector.connect(
    host='localhost',
    user='your_username',
    password='your_password',
    database='your_database'
)

# Check whether the connection was successful
if conn.is_connected():
    print('Connected to the MySQL database.')

# Close the connection
conn.close()

මෙම කේතයේදී නියමිත host, පරිශීලක නාමය, මුරපදය, දත්ත ගබඩා නාමය භාවිතා කර MySQL සමඟ සම්බන්ධතාවය ස්ථාපනය වේ. is_connected() ක්‍රමය භාවිතා කර සම්බන්ධතාවය සාර්ථකදැයි පරීක්ෂා කළ හැක. ඔබට සම්බන්ධතාවය අවශ්‍ය නැති වූ විට close() ක්‍රමය භාවිතා කර එය වසා දැමීමට අමතක නොකරන්න.

4. දත්ත ගබඩා මෙහෙයුම් මූලිකතා

MySQL Connector/Python භාවිතා කර දත්ත ගබඩාවට සම්බන්ධ වීම සාර්ථක වූ පසු, ඊළඟ පියවර වන්නේ මූලික දත්ත ගබඩා මෙහෙයුම් සිදු කිරීමයි. මෙම කොටස තුළ වගු (tables) නිර්මාණය, දත්ත ඇතුළත් කිරීම, ලබා ගැනීම, යාවත්කාලීන කිරීම, මකා දැමීම වැනි ක්‍රියාමාර්ග පිළිබඳ විස්තර කර ඇත.

4.1 වගුවක් නිර්මාණය කිරීම

පළමුව දත්ත ගබඩාවේ නව වගුවක් නිර්මාණය කරන ආකාරය බලමු. පහත කේතය users නමැති වගුවක් නිර්මාණය කිරීමේ උදාහරණයකි.

# Get a cursor
cursor = conn.cursor()

# Query to create a table
create_table_query = '''
CREATE TABLE IF NOT EXISTS users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(255) NOT NULL,
    email VARCHAR(255) NOT NULL
)
'''

# Create the table
cursor.execute(create_table_query)

This code creates the users table only if it does not already exist. The id column is the primary key and is set to auto-increment.

4.2 Inserting Data

Next, let’s insert data into the table.

# Query to insert data
insert_data_query = '''
INSERT INTO users (username, email) VALUES (%s, %s)
'''

# Data to insert
user_data = ("Tanaka", "tanaka@example.com")

# Insert the data
cursor.execute(insert_data_query, user_data)

# Commit the changes
conn.commit()

This code inserts a new user’s data into the users table. The %s values are placeholders that will be replaced with the data you provide.

4.3 Retrieving Data

Let’s also look at how to retrieve data from the table.

# Query to retrieve data
select_query = "SELECT * FROM users WHERE username = %s"

# Retrieve the data
cursor.execute(select_query, ("Tanaka",))

# Fetch the result
result = cursor.fetchone()
print(result)

This code retrieves the record from the users table where the username is Tanaka.

4.4 Updating Data

Here is how to update existing data.

# Query to update data
update_query = "UPDATE users SET email = %s WHERE username = %s"

# Update the data
cursor.execute(update_query, ("tanaka.new@example.com", "Tanaka"))

# Commit the changes
conn.commit()

This code updates Tanaka’s email address to a new one.

4.5 Deleting Data

Finally, here is how to delete data.

# Query to delete data
delete_query = "DELETE FROM users WHERE username = %s"

# Delete the data
cursor.execute(delete_query, ("Tanaka",))

# Commit the changes
conn.commit()

This code deletes the record from the users table where the username is Tanaka.

5. Placeholders and Prepared Statements

In MySQL Connector/Python, you can improve security and performance by using placeholders and prepared statements when executing SQL queries. By leveraging these features, you can reduce security risks such as SQL injection and improve query execution efficiency.

5.1 Using Placeholders

By using placeholders, you can dynamically specify values inside an SQL query. This is also an effective way to prevent SQL injection. Below is an example of an SQL query using placeholders.

# Query to retrieve data
select_query = "SELECT * FROM users WHERE username = %s"

# Execute the query using a placeholder
cursor.execute(select_query, ("Tanaka",))

# Fetch the result
result = cursor.fetchone()
print(result)

Here, %s is the placeholder, and it will be replaced with the value provided as the second argument to the execute method. With this approach, the input value is automatically escaped, helping to prevent SQL injection.

5.2 Using Prepared Statements

A prepared statement is a technique that improves performance when executing the same query multiple times. The SQL statement is parsed once at the beginning, and subsequent executions can skip parsing, making the process faster. Below is an example of how to use prepared statements in MySQL Connector/Python.

# Create a cursor (prepared statement enabled)
cursor = conn.cursor(prepared=True)

# Prepared statement query
stmt = "SELECT * FROM users WHERE username = ?"

# Execute the query
cursor.execute(stmt, ("Tanaka",))

# Fetch the result
result = cursor.fetchone()
print(result)

By specifying prepared=True when creating the cursor, prepared statements are enabled. Also, note that prepared statements use ? instead of %s as the placeholder.

Benefits of Prepared Statements

  • ආරක්ෂාව : SQL ඉන්ජෙක්ශන් වලින් ආරක්ෂා කරයි, placeholders වැනිව.
  • කාර්ය සාධනය : එකම විමසුම නැවත නැවත ක්‍රියාත්මක කරන විට, විග්‍රහය එකවර පමණක් සිදු වන බැවින් කාර්යක්ෂමතාව වැඩි කරයි.

6. Escaping සහ සම්බන්ධතාවයෙන් SQL ක්‍රියාත්මක කිරීම

Escaping අවශ්‍ය වේ ගතිකව SQL විමසුම් සෑදීමේදී හෝ විශේෂ අක්ෂර අඩංගු දත්ත සැකසීමේදී. MySQL Connector/Python Escaping හසුරවීමට පහසු ක්‍රියාකාරකම් ලබා දෙයි.

6.1 Escaping

MySQL Connector/Python සමඟ, ඔබට Escaping අතින් ක්‍රියාත්මක කිරීමට අවශ්‍ය නැත. සම්බන්ධතා වස්තුවේ converter.escape ක්‍රියාවලිය භාවිතා කරමින් අගයන් පහසුවෙන් ඉස්කේප් කළ හැක. පහත උදාහරණය එකක් උපුටා ගැනීම (single quote) ඇතුළත් දත්ත ආරක්ෂිතව හසුරවන්නේ කෙසේද යන්න පෙන්වයි.

# Example of escaping
escaped_string = conn.converter.escape("O'Reilly")
print(escaped_string)  # Output: O'Reilly

මෙම ආකාරයෙන් අගයන් ඉස්කේප් කිරීමෙන්, විශේෂ අක්ෂර ඇතුළත් වුවත් ආරක්ෂිතව SQL විමසුම් සෑදිය හැක.

6.2 සම්බන්ධතාවයෙන් SQL සෘජුව ක්‍රියාත්මක කිරීම

සාමාන්‍යයෙන්, ඔබ SQL විමසුම් cursor (කර්සර) භාවිතා කර ක්‍රියාත්මක කරයි. එහෙත්, සමහර අවස්ථාවල, සම්බන්ධතා වස්තුවේ cmd_query ක්‍රමය භාවිතා කර SQL සෘජුව ක්‍රියාත්මක කළ හැක. එහෙත්, මෙම ක්‍රමය placeholders (ස්ථානදාරක) සඳහා සහය නොදක්වයි, එබැවින් ඔබට Escaping ඔබම කළ යුතුය, එබැවින් අවධානයෙන් භාවිතා කළ යුතුය.

# Execute an SQL query directly
stmt = "SELECT * FROM users WHERE username = '%s'"
conn.cmd_query(stmt % conn.converter.escape("Tanaka"))

මෙම ක්‍රමය භාවිතා කරන විට, %s වටා උද්ධෘත ලකුණු (quotes) නිවැරදිව තබා Escaping නිසි ලෙස යෙදිය යුතුය. බොහෝ භාවිතයන් සඳහා, placeholders සමඟ cursor එකක් භාවිතා කිරීම නිර්දේශ කරයි.

7. දෝෂ හසුරවීම සහ දත්ත ගබඩා හොඳ පුරුදු

දත්ත ගබඩා මෙහෙයුම් අතර දෝෂ සිදුවිය හැක, එබැවින් නිසි දෝෂ හසුරවීම අත්‍යවශ්‍ය වේ. දත්ත ගබඩාව ආරක්ෂිතව හා කාර්යක්ෂමව මෙහෙයවීමට හොඳ පුරුදු (best practices) දැන ගැනීමත් වැදගත්.

7.1 දෝෂ හසුරවීම ක්‍රියාත්මක කිරීම

MySQL Connector/Python භාවිතා කරන විට, දෝෂ අල්ලා නිසි ලෙස හසුරවීමට try-except බ්ලොක් එකක් භාවිතා කිරීම නිර්දේශ කරයි. පහත දෝෂ හසුරවීම ක්‍රියාත්මක කිරීමේ උදාහරණයක් ඇත.

import mysql.connector
from mysql.connector import Error

try:
    conn = mysql.connector.connect(
        host='localhost',
        user='your_username',
        password='your_password',
        database='your_database'
    )
    cursor = conn.cursor()
    cursor.execute("SELECT * FROM users")
    results = cursor.fetchall()
    for row in results:
        print(row)

except Error as e:
    print(f"An error occurred: {e}")

finally:
    if conn.is_connected():
        cursor.close()
        conn.close()
        print("The MySQL connection has been closed.")

try බ්ලොක් තුළ දත්ත ගබඩා මෙහෙයුම් සිදු කරයි. except බ්ලොක් තුළ දෝෂ අල්ලා සුදුසු පණිවුඩයක් පෙන්වයි. අවසානයේ finally බ්ලොක් සම්බන්ධතාවය වසා දැමීමට සහතික වේ. මෙය දෝෂයක් සිදු වුවත් වැඩසටහන සම්බන්ධතාවය ආරක්ෂිතව අවසන් කිරීමට ඉඩ සලසයි.

7.2 දත්ත ගබඩා හොඳ පුරුදු

  • සම්බන්ධතා කළමනාකරණය : දත්ත ගබඩා සම්බන්ධතා සම්පත් භාවිතා කරයි, එබැවින් අවශ්‍ය නොවන විට සෑම විටම ඒවා වසා දැමිය යුතුය.
  • ස්ථානදාරක (placeholders) භාවිතා කිරීම : SQL ඉන්ජෙක්ශන් වලින් ආරක්ෂා වීමට, SQL විමසුම් වල අගයන් සඳහා සෑම විටම ස්ථානදාරක භාවිතා කරන්න.
  • අපවාද (exceptions) හසුරවීම : දත්ත ගබඩා මෙහෙයුම් අතර දෝෂ සිදුවිය හැක, එබැවින් try-except බ්ලොක් භාවිතා කර ඒවා නිසි ලෙස හසුරවන්න.
  • ගනුදෙනු (Transactions) : දත්ත අඛණ්ඩතාව රැක ගැනීමට, අවශ්‍ය විට ගනුදෙනු භාවිතා කර, බහු මෙහෙයුම් එකම ඒකකයක් ලෙස commit හෝ rollback කළ හැක.

8. සාරාංශය

MySQL Connector/Python යනු Python භාවිතා කර MySQL දත්ත ගබඩා සම්බන්ධ වීමට සහ ඒ සමඟ වැඩ කිරීමට ශක්තිමත් මෙවලමකි. මෙම ලිපියේ, MySQL Connector/Python සකස් කිරීම, මූලික දත්ත ගබඩා මෙහෙයුම් සිදු කිරීම, ආරක්ෂාව වැඩි කිරීමට placeholders සහ prepared statements (පූර්ව-සූදානම් ප්‍රකාශ) භාවිතා කිරීම, Escaping සහ දෝෂ හසුරවීම නිසි ලෙස කළ හැකි ආකාරය ගැන කතා කළෙමු. මෙම තාක්ෂණයන් පරිපූර්ණව අධ්‍යයනය කිරීමෙන්, ඔබට වැඩි කාර්යක්ෂම සහ ආරක්ෂිත දත්ත ගබඩා යෙදුම් නිර්මාණය කළ හැක.

ඊළඟ පියවර ලෙස, MySQL Connector/Python සමඟ වැඩි ප්‍රායෝගික උදාහරණ සහ උසස් දත්ත ගබඩා මෙහෙයුම් පරීක්ෂා කර බලන්න. MySQL Connector/Python පිළිබඳ නිල ලේඛන සහ සම්බන්ධිත සම්පත් භාවිතා කර එහි ලක්ෂණවලින් උපරිම ප්‍රයෝජනය ලබා ගැනීමට ගැඹුරු අවබෝධයක් ලබා ගැනීමත් වැදගත් වේ.