Whenever I try to create a table the text stays as a string, im I doing something wrong?
Remove the last comma, just after social integer
c.execute("""CREATE TABLE personalinfo (
name text,
lastname text,
age integer,
social integer
)""")
Related
How can I select the company name (Acme,Foo) instead it's company_id when inserting a new person?
Here I can select 1 or 2 but want to see Acme or Foo instead. Alas picture doesn't show the dropdown displaying the company ids:
CREATE TABLE "company" (
"company_id" INTEGER,
"name" TEXT,
PRIMARY KEY("company_id" AUTOINCREMENT)
)
CREATE TABLE "person" (
"last_name" TEXT NOT NULL,
"firstname" TEXT,
"salutation" BLOB,
"company_id" INTEGER,
"person_id" INTEGER,
FOREIGN KEY("company_id") REFERENCES "company"("company_id"),
PRIMARY KEY("person_id" AUTOINCREMENT)
)
I'm attempting to use the db browser as crm gui.
Closing this question as duplicate without context gives me no direction at all. How does that question help in regards of inserting a new row using the sqlitebrowser?
I'm developing an iOS app and I have a sqlite database with 2 tables related by 1-to-many relationship.
Now I would like to do a query that retrieve all element by first table and in the same time do a count by second table so I can pass the result into my view.
CREATE TABLE track(
trackid INTEGER,
trackname TEXT,
trackartist INTEGER,
FOREIGN KEY(trackartist) REFERENCES artist(artistid)
);
CREATE TABLE artist(
artistid INTEGER PRIMARY KEY,
artistname TEXT
);
I would like to create a query that returns all artist name and the count of track for each artist name so I can pass this value to my list.
Is it possible? Any help?
Thanks to Joe, your code works well for my, but it's possibile to add new field for store the result of count?
Sorry and if i would take the also all trackname for each artist in the same query?
SELECT a.artistname, count(*)
FROM track t
INNER JOIN artist a
on t.trackartist = a.artistid
GROUP BY a.artistid
Try this:
SELECT a.artistname,
(SELECT COUNT(*) FROM track t
WHERE t.trackartist = a.artistid)
FROM artist a
This should be an easy one for someone with more SQLite experience than myself.
I need a select statement to get the name out of the following example string:
{"email":"12345678#facebook.com","user_key":"FACEBOOK:12345678","name":"John Smith"}
The output I need is John Smith.
The number of characters before the name is not always the same so a simple substr command won't work. It needs to be dynamic so it can locate where the name starts and then spit it out. I think ltrim or rtrim may help, but even after researching those commands, I don't understand them very well. Also, SQLite doesn't offer instr or position, which might have been helpful, too!
Edit: the schema for this table is as follows:
CREATE TABLE messages (msg_id TEXT PRIMARY KEY, thread_id TEXT, action_id INTEGER, subject TEXT, text TEXT, sender TEXT, timestamp_ms INTEGER, timestamp_sent_ms INTEGER, attachments TEXT, shares TEXT, msg_type INTEGER, affected_users TEXT, coordinates TEXT, offline_threading_id TEXT, source TEXT, channel_source TEXT, is_non_authoritative INTEGER, pending_send_media_attachment STRING, handled_internally_time INTEGER, pending_shares STRING, pending_attachment_fbid STRING, client_tags TEXT, send_error STRING, send_error_message STRING, send_error_timestamp_ms INTEGER, publicity TEXT, tracking TEXT );
CREATE INDEX messages_offline_threading_id_index ON messages ( offline_threading_id );
CREATE INDEX messages_timestamp_index ON messages ( thread_id, timestamp_ms DESC );
CREATE INDEX messages_type_index ON messages ( thread_id, msg_type, timestamp_ms );
The string I have above that I'm working with is from the sender column.
I don't know the language you're using but most of them support user defined functions (http://www.sqlite.org/c3ref/create_function.html) and you could do
select jsonGetName(columnWithJsonText) from messages where ...
It's like a callback to your programming language where you defined which C/Java/PHP function gets called when using jsonGetName() in sqlite.
In that function (in your language of choice) you decode the json string and return the name property.
Having an issue where my queries used to work fine with = as in WHERE some_int_field = some_other_int_field. When I do that now I get 0 results. However if I do a WHERE some_int_field LIKE some_other_int_field I get my results. I have checked the length of the fields for hidden characters/spaces and the length of the fields are correct. They are both integer fields. Thoughts? Two tables structure below:
CREATE TABLE "languages"(
"language_id" Integer,
"name" Text,
"english" Text,
"spanish" Text,
"portuguese" Text,
"french" Text );
-- Create index languagesIdx
CREATE INDEX "languagesIdx" ON "languages"( "name" );
BEGIN;
-------------
CREATE TABLE "drop_downs"(
"mode_data" Integer,
"text_index" Integer,
"language_id" Integer );
-- Create index drop_downsIdx
CREATE INDEX "drop_downsIdx" ON "drop_downs"( "mode_data", "language_id" );
BEGIN;
SQLite uses dynamic typing and does not care about the declared type of the fields.
You have strings in your fields.
To check which rows have strings, use something like this:
SELECT * FROM drop_downs WHERE typeof(mode_data) = 'text'
To convert all values in a column into numbers, use something like this:
UPDATE drop_downs SET mode_data = CAST(mode_data AS integer)
So I have 1 table that I created a with new constraints and trying to move the data from the old table into the new one but it is not working, it does not give an error or anything.
I created old table with:
CREATE TABLE Elements (
_id INTEGER PRIMARY KEY,
Element TEXT,
Symbol TEXT,
MW TEXT)
New Table created with:
CREATE TABLE "Element" (
"_id" INTEGER PRIMARY KEY AUTOINCREMENT,
"ElementName" TEXT,
"Symbol" TEXT,
"MW" REAL,
UNIQUE("ElementName") ON CONFLICT ABORT,
UNIQUE("Symbol") ON CONFLICT ABORT)
These are the statements to move the data over but they all did not insert anything:
INSERT INTO "Element" ("ElementName","Symbol","MW")
SELECT "Element", "Symbol","MW"
FROM "Elements"
INSERT INTO "Element"
SELECT * FROM "Elements"
INSERT INTO Element (ElementName,Symbol,MW)
SELECT Element,Symbol,CAST(MW AS REAL) FROM Elements
INSERT INTO Element (_id,ElementName,Symbol,MW)
SELECT NULL,Element,Symbol,MW
FROM Elements
I cannot figure it out. Can someone possible tell me what is wrong with my insert statements to move the data. The biggest change was the constraints on the table.
I tried and this worked for me.
INSERT INTO Element SELECT * FROM Elements
just removed "" from Element and Elements
The problem was fixed by deleting the table and recreating it again without all the quotes in it. I do not know if this is a bug in SQLite Database Browser or what. I used the statements below and it ran fine:
CREATE TABLE Element (
_id INTEGER PRIMARY KEY AUTOINCREMENT,
ElementName TEXT,
Symbol TEXT,
MW REAL,
UNIQUE(ElementName) ON CONFLICT ABORT,
UNIQUE(Symbol) ON CONFLICT ABORT)
INSERT INTO Element SELECT * FROM Elements
How about:
INSERT INTO Element (ElementName, Symbol, MW)
SELECT o.Element, o.Symbol, o.MW
FROM Elements as o
If that fails, please compare these outputs:
SELECT COUNT(*) FROM Elements;
SELECT COUNT(Element) FROM (SELECT DISTINCT Element FROM Elements);
SELECT COUNT(Symbol) FROM (SELECT DISTINCT Symbol FROM Elements);