相同的代码不同的表名称给出错误!在卡桑德拉

发布于 2025-01-15 14:42:35 字数 2464 浏览 1 评论 0原文

在 Jupyter Notebook 中使用 apache Cassandra、创建表和插入数据都可以正常工作。我分别更改表名称后,给出错误!

工作第一代码:

session.execute("""CREATE TABLE IF NOT EXISTS table2 
(artist text , song text, firstname text , lastname text, userId int ,sessionid int, iteminsession int , 
PRIMARY KEY ((userId, sessionId), itemInSession)) """)


file = 'event_datafile_new.csv'    

with open(file, encoding = 'utf8') as f:
   csvreader = csv.reader(f)
   next(csvreader) # skip header
   for line in csvreader:
       
       query = "INSERT INTO table2 (artist, song, firstname, lastname , userId, sessionId, itemInSession)"
       query = query + "VALUES (%s, %s, %s, %s ,%s ,%s ,%s)"
       
   
       # building the insert
       # executing the insertion
       session.execute(query , (line[0], line[9], line[1], line[4], int(line[10]), int(line[8]), int(line[3])) )
         



query = "select artist, song, firstname, lastname from table2 WHERE userId= 10 and sessionId = 182"

try:
   rows = session.execute(query)
except Exception as e:
   print(e)
   
for row in rows:
      print(f'artist: {row.artist}, song: {row.song}, user first name: {row.firstname},user last name: {row.lastname}') 

将表名称从“table2”更改为“artist_song”出现错误:InvalidRequest:来自服务器的错误:code=2200 [无效查询] message =“未定义的列名称firstname”

session.execute("""CREATE TABLE IF NOT EXISTS artist_song
(artist text , song text, firstname text , lastname text, userId int ,sessionid int, iteminsession int , 
PRIMARY KEY ((userId, sessionId), itemInSession)) """)


file = 'event_datafile_new.csv'    

with open(file, encoding = 'utf8') as f:
    csvreader = csv.reader(f)
    next(csvreader) # skip header
    for line in csvreader:
        
        query = "INSERT INTO artist_song (artist, song, firstname, lastname , userId, sessionId, itemInSession)"
        query = query + "VALUES (%s, %s, %s, %s ,%s ,%s ,%s)"
        
    
        # building the insert
        # executing the insertion
        session.execute(query , (line[0], line[9], line[1], line[4], int(line[10]), int(line[8]), int(line[3])) )
          


query = "select artist, song, firstname, lastname from artist_song WHERE userId= 10 and sessionId = 182"

try:
    rows = session.execute(query)
except Exception as e:
    print(e) 
for row in rows:
       print(f'artist: {row.artist}, song: {row.song}, user first name: {row.firstname},user last name: {row.lastname}')

working with apache Cassandra in Jupyter Notebook, creating a table, and inserting data all works fine. after I change the Table name, respectively, gives an error!

the working first code:

session.execute("""CREATE TABLE IF NOT EXISTS table2 
(artist text , song text, firstname text , lastname text, userId int ,sessionid int, iteminsession int , 
PRIMARY KEY ((userId, sessionId), itemInSession)) """)


file = 'event_datafile_new.csv'    

with open(file, encoding = 'utf8') as f:
   csvreader = csv.reader(f)
   next(csvreader) # skip header
   for line in csvreader:
       
       query = "INSERT INTO table2 (artist, song, firstname, lastname , userId, sessionId, itemInSession)"
       query = query + "VALUES (%s, %s, %s, %s ,%s ,%s ,%s)"
       
   
       # building the insert
       # executing the insertion
       session.execute(query , (line[0], line[9], line[1], line[4], int(line[10]), int(line[8]), int(line[3])) )
         



query = "select artist, song, firstname, lastname from table2 WHERE userId= 10 and sessionId = 182"

try:
   rows = session.execute(query)
except Exception as e:
   print(e)
   
for row in rows:
      print(f'artist: {row.artist}, song: {row.song}, user first name: {row.firstname},user last name: {row.lastname}') 

changing table name from 'table2' to 'artist_song' getting error: InvalidRequest: Error from server: code=2200 [Invalid query] message="Undefined column name firstname"

session.execute("""CREATE TABLE IF NOT EXISTS artist_song
(artist text , song text, firstname text , lastname text, userId int ,sessionid int, iteminsession int , 
PRIMARY KEY ((userId, sessionId), itemInSession)) """)


file = 'event_datafile_new.csv'    

with open(file, encoding = 'utf8') as f:
    csvreader = csv.reader(f)
    next(csvreader) # skip header
    for line in csvreader:
        
        query = "INSERT INTO artist_song (artist, song, firstname, lastname , userId, sessionId, itemInSession)"
        query = query + "VALUES (%s, %s, %s, %s ,%s ,%s ,%s)"
        
    
        # building the insert
        # executing the insertion
        session.execute(query , (line[0], line[9], line[1], line[4], int(line[10]), int(line[8]), int(line[3])) )
          


query = "select artist, song, firstname, lastname from artist_song WHERE userId= 10 and sessionId = 182"

try:
    rows = session.execute(query)
except Exception as e:
    print(e) 
for row in rows:
       print(f'artist: {row.artist}, song: {row.song}, user first name: {row.firstname},user last name: {row.lastname}')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文