def insert_movies(): with open('./ml-latest-small/movies.csv', newline='', encoding="utf8") as csvfile: spamreader = csv.reader(csvfile) for row in spamreader: # Prepare SQL query to INSERT a record into the database. sql = "INSERT INTO movies(id,title, geners) VALUES ('%s', '%s', '%s' );" % (row[0], row[1], row[2]) print(sql) try: # Execute the SQL command mycursor.execute(sql) # Commit your changes in the database connection.commit() except: # Rollback in case there is any error connection.rollback()
def insert_movies(): with open('./ml-latest-small/movies.csv', newline='', encoding="utf8") as csvfile: spamreader = csv.reader(csvfile) for row in spamreader: # Prepare SQL query to INSERT a record into the database. sql = "INSERT INTO movies(id,title, geners) VALUES ('%s', '%s', '%s' );" % ( row[0], row[1], row[2]) print(sql) try: # Execute the SQL command mycursor.execute(sql) # Commit your changes in the database connection.commit() except: # Rollback in case there is any error connection.rollback()
" `ratings` float(50) NOT NULL," " `timestapm` TIMESTAMP DEFAULT 0" ") ENGINE=InnoDB") TABLES['tags'] = ("CREATE TABLE `tags` (" " `user_id` int(50) NOT NULL," " `movie_id` int(50) NOT NULL," " `tags` varchar(150) NOT NULL," " `timestapm` TIMESTAMP DEFAULT 0" ") ENGINE=InnoDB") # connector = do_connect() # connector.execute() #mycursor.execute("CREATE DATABASE WWWython") #mycursor.execute("USE python") for name, ddl in TABLES.items(): try: print("Creating table {}: ".format(name), end='') mycursor.execute(ddl) except mysql.connector.Error as err: if err.errno == errorcode.ER_TABLE_EXISTS_ERROR: print("already exists.") else: print(err.msg) else: print("OK") mycursor.close() connection.close()
TABLES['ratings'] = ( "CREATE TABLE `ratings` (" " `user_id` int(50) NOT NULL," " `movie_id` int(50) NOT NULL," " `ratings` float(50) NOT NULL," " `timestapm` varchar(250) NOT NULL" ") ENGINE=InnoDB") TABLES['tags'] = ( "CREATE TABLE `tags` (" " `user_id` int(50) NOT NULL," " `movie_id` int(50) NOT NULL," " `tags` varchar(250) NOT NULL," " `timestapm` varchar(250) NOT NULL" ") ENGINE=InnoDB") for name, ddl in TABLES.items(): try: print("Creating table {}: ".format(name), end='') mycursor.execute(ddl) except mysql.connector.Error as err: if err.errno == errorcode.ER_TABLE_EXISTS_ERROR: print("already exists.") else: print(err.msg) else: print("OK") mycursor.close() connection.close()
# INTO TABLE discounts # FIELDS TERMINATED BY ',' # ENCLOSED BY '"' # LINES TERMINATED BY '\n' # IGNORE 1 ROWS; # def insert_movies(): # header = ('title', 'geners') # with open('./ml-latest-small/movies.csv', newline='', encoding="utf8") as csvfile: # spamreader = csv.reader(csvfile) # for row in spamreader: # movie = dict(zip(header, row[1:])) # print(spamreader) # # Movie.objects.create(**movie) with open('./ml-latest-small/movies.csv', newline='', encoding="utf8") as csvfile: spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|') for row in spamreader: # sql = "INSERT INTO testSmall VALUES (%s);" % ', '.join('?' for _ in row) # mycursor.execute (sql, row) # sql = "INSERT INTO `movies` (`title`, `geners`) VALUES ( ?, ?);" mycursor.execute( "INSERT INTO `movies` (`id`, `title`, `geners`) VALUES ( %s, %s, %s);", row) #close the connection to the database. connection.commit() mycursor.close() # mycursor.execute (sql, (row[1], row[2])) # print(', '.join(row))