#๐ How can I retrieve id database
18 messages ยท Page 1 of 1 (latest)
@delicate sphinx
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Image reference
Are you using SQLAlchemy?
Is this tkinter?
I'm using toga and sqlite
code?
from pathlib import Path
DB_PATH = Path.home() / "ci_dian.db"
def create_database():
"""Create the database and initialize the users table."""
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
email TEXT NOT NULL UNIQUE
)
''')
conn.commit()
conn.close()
def add_user(name, email):
"""Add a new user to the database."""
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute("INSERT INTO users (name, email) VALUES (?, ?)", (name, email))
conn.commit()
conn.close()
def update_user(user_id, new_name, new_email):
"""Update the user's information in the database."""
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute('''
UPDATE users
SET name = ?, email = ?
WHERE id = ?
''', (new_name, new_email, user_id))
conn.commit()
conn.close()
def get_users():
"""Retrieve all users from the database."""
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute("SELECT * FROM users")
users = cursor.fetchall()
conn.close()
return users ```
here is the backend
And your toga code?
You can use a pastebin if its too long
If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/
After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.
I've not used toga but do these values exist when you trigger the edit_user function?
if self.selected_row:
user_id = self.selected_row.data[0] # Retrieve the user ID from the selected row
new_name = self.name_input.value.strip() # Get the new name from the input
new_email = self.email_input.value.strip() # Get the new email from the input
Can you print them out?
nope it give me an error Error in handler: SendyKurniawan.on_row_select() missing 1 required positional argument: 'row' Traceback (most recent call last): File "C:\Python312\Lib\site-packages\toga\handlers.py", line 173, in _handler result = handler(interface, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: SendyKurniawan.on_row_select() missing 1 required positional argument: 'row'
maybe you don't need the row in on_row_select? idk tbh
Lemme test
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.