# 執行 SQL 腳本來建立資料庫 withopen('Chinook_Sqlite.sql', 'r') as f: cursor.executescript(f.read())
# 提交變更並關閉連接 conn.commit() conn.close()
查詢資料表清單可以看到這個範例資料庫裡面包含了總共有11張資料表
1 2 3 4
con = sqlite3.connect("Chinook.db") cursorObj = con.cursor() cursorObj.execute("SELECT name FROM sqlite_master WHERE type='table';") print(cursorObj.fetchall())
import os import getpass import google.generativeai as genai
prompt = """ Please convert question into SQL query by following table and column information. table: Employee column: EmployeeId, LastName, FirstName, Title, ReportsTo, BirthDate, HireDate, Address, City, State, Country, PostalCode, Phone, Fax, Email Question: How many employees come from Canada? """
# Configure Gemini GEMINI_API_KEY = getpass.getpass() # input your gemini api key genai.configure(api_key=GEMINI_API_KEY)
model = genai.GenerativeModel('gemini-1.5-flash') response = model.generate_content(prompt)
print(response.text)
可以看到Gemini依據prompt生成了一段SQL,直接讀這段SQL也有符合前面給的問題。
1 2 3 4
>>> SELECTCOUNT(*) AS "Number of Canadian Employees" FROM Employee WHERE Country ='Canada';