Working on some GhPython script to extract data from a SQLite database.
In the Python component’s preview, I get the string results of my queries just fine, but when the output is displayed in a panel, all the accents get the unicode treatment.
I’ve tried to interpret solutions found here and there, but with no success.
The reason that you get the weird looking strings is that you’re printing the result tuples instead of the string you want.
The hint is already in your own printing code where you do:
for item in items:
print item[0] ## << see, you're getting the first item of the result tuple
Fixed version:
# -*- coding: utf-8 -*-
__author__ = "osuir, and small correction by jesterKing"
__version__ = "2021.01.05"
import rhinoscriptsyntax as rs
import Rhino as rh
import sqlite3 as sq
import os
db_file_path = r'C:\Users\Nathan\Downloads\Test accents.db'
conn = sq.connect(db_file_path)
# Create a cursor
c = conn.cursor()
# Query the database and return all records with a sub-routine
c.execute
# Get the strings only
items = [i[0] for i in c.fetchall()]
a = items("""SELECT bogus_field
FROM BOGUS_TABLE""")
# Close the connection
conn.close()
How the query is done doesn’t really matter. It matters how you handle the results. Passing on a tuple instead of the string you intended is what your original problem was.
I’m going to guess that your test database is just one column with all the records, and the current code just executes essentially a SELECT *;
Side question : how come I don’t see dozens of threads about SQLigth in this forum ?
It seems so fitting to use the power of databases in GH… but no one seems interested.
There are some questions about it every now and then. There’s not much more to say than what one can glean from great coding resources like stackoverflow.com and such. There’s loads of info on SQLite out there
@osuire, I already see what the confusion here is. I must have made a copy/paste error while copying the new code to Discourse, probably fatfingered the wrong buttons, thus accidently removing the actual query. I’ve fixed the reply with the correct Python code.
Nathan, one of the reasons so many people enjoy GH is that they like to program visually, and do without the hassle of syntax script stickling.
See here, for example, I’m still struggling because for some reason, my version of the script now refuses to output a single thing.
If I had a component with a “Database path” input, a few “SQL query” inputs and the corresponding outputs, I’d be fine. I could focus on getting my job done instead of wondering about utf8, if I need one " or three, if my indentation is good, if I forgot (god forbid !) to type a “:” somewhere, etc…