在Python Sqlite3中向同一列添加多个值

2024-05-19 15:39:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建了一个表,在一个简单的社交媒体网站中存储每个用户的信息和数据。我有一列“friends”,我希望这个列有多个用逗号分隔的值。例如,“约翰、玛丽亚、蝙蝠侠等等”。很快,每当用户单击Follow按钮时,另一个人的名字就会被添加到“friends”列中

with sqlite3.connect('memory.db') as conn:
     cursor = conn.cursor()
     cursor.execute(
         "CREATE TABLE users (user_id char(50), name char(50), email char(50), password char(30), "
         "university char(50), birthday char(50), age char(10), hometown char(50), photo, status char(50), friends char(1000));")

     name = session['name']  #the name of the user
     name_search = session['name_search']  #the name of the person who we want to follow
     if name != name_search:  
         cursor.execute('''UPDATE users SET friends=? WHERE name=?''', (name_search, name))
         conn.commit()

我使用了UPDATE方法,但它只使用一个值更新列


Tags: ofthe用户nameexecutesearchsessionupdate