SQLITE3 programming learning in Python - database

I've created a new sql base in pycharm and it gave me the next error message:
Traceback (most recent call last):
File "C:\Users\rnicolescu\Desktop\my_program.py", line 619 (line from the pycharm), in
cursor.execute("""INSERT INTO Names (id, firstname, surname, phonenumber)
sqlite3.OperationalError: table Names has no column named surname
and the code writen is as follows:
import sqlite3
with sqlite3.connect("Phonebook.db") as db:
cursor = db.cursor()
cursor.execute("""CREATE TABLE IF NOT EXISTS Names (
id integer PRIMARY KEY,
firstname text,
surname text,
phonenumber text);""")
cursor.execute("""INSERT INTO Names (id, firstname, surname, phonenumber)
VALUES ('1', 'Simon', 'Robert', '0725383561')""")
db.commit()
cursor.execute("""INSERT INTO Names (id, firstname, surname, phonenumber)
VALUES ('2', 'Robert', 'Mihai', '0725333698')""")
db.commit()
cursor.execute("""INSERT INTO Names (id, firstname, surname, phonenumber)
VALUES ('3', 'Marc', 'Aurelius', '0122238569')""")
db.commit()
cursor.execute("""INSERT INTO Names (id, firstname, surname, phonenumber)
VALUES ('4', 'Iulian', 'Constantin', '3532644226')""")
db.commit()
db.close()
Where should I look for the error? I can't figure it out. Thanks

Related

Comapre sqlserver table with the query results

I have a table 'A' with few columns say - ID, firstname,lastname,address and phonenumber.
I am populating this table on a daily basis using a query that joins few tables.
Now before loading the table, I need to check my query results against the table data and see if the data is already present.
If my table data and query results matches, then no need to insert the record.
if my table data and query result doesn't match then need to insert the record.
If the table data and the query result has updated record, then the data has to be inserted.
that is, If the Id,lastname,firstname, address are all same and only the PhoneNumber is changes in my query result. Then this record should also be inserted in the table A.
I tried a query to identify the Point -3, But I'm stuck how to proceed further and make thhose records to be inserted in table A.
SELECT DISTINCT *
FROM
(
SELECT * FROM
( SELECT [ID],FirstName, LastName,[PhoneNumber],[ExpDate] FROM [SCRATCH].[dbo].[A]
UNION ALL
SELECT D.[ID],FirstName, LastName,,[PhoneNumber],[ExpDate] FROM
S JOIN D ON D.ID = S.ID) Tbls
GROUP BY [ID],FirstName, LastName,[PhoneNumber],[ExpDate]
HAVING COUNT(*)<2) Diff
To get a list of rows that don't match between two tables - you can use EXCEPT. For example:
SELECT [ID],FirstName, LastName,[PhoneNumber],[ExpDate] FROM [SCRATCH].[dbo].[A]
Except
SELECT D.[ID],FirstName, LastName,,[PhoneNumber],[ExpDate] FROM S
From this - we can then construct an insert statement, here is an example with data:
Declare #tableS Table (ID int, FirstName varchar(30), LastName varchar(30), PhoneNumber varchar(12), ExpDate date);
Insert Into #tableS (ID, FirstName, LastName, PhoneNumber, ExpDate)
Values (1, 'Test', 'Testing', '111-111-1111', '2021-03-01');
Declare #tableA Table (ID int, FirstName varchar(30), LastName varchar(30), PhoneNumber varchar(12), ExpDate date)
Insert Into #tableA (ID, FirstName, LastName, PhoneNumber, ExpDate)
Values (2, 'Test', 'Testing', '222-222-2222', '2021-03-01')
, (3, 'Test', 'Testing', '333-333-3333', '2021-03-01');
--==== Insert new/changed rows into #tableS
Insert Into #tableS (ID, FirstName, LastName, PhoneNumber, ExpDate)
Select ID, FirstName, LastName, PhoneNumber, ExpDate From #tableA
Except
Select ID, FirstName, LastName, PhoneNumber, ExpDate From #tableS;
Select * From #tableS;
Insert Into #tableA (ID, FirstName, LastName, PhoneNumber, ExpDate)
Values (1, 'Test', 'Testing', '111-222-3333', '2021-03-02')
, (4, 'Test', 'Testing', '444-444-4444', '2021-03-04');
--==== Insert new/changed rows into #tableS
Insert Into #tableS (ID, FirstName, LastName, PhoneNumber, ExpDate)
Select ID, FirstName, LastName, PhoneNumber, ExpDate From #tableA
Except
Select ID, FirstName, LastName, PhoneNumber, ExpDate From #tableS;
Select * From #tableS;
You probably will need to adjust the columns used with EXCEPT so you are not comparing the ID and the ExpDate - but this should get you started.

Reading non latin character from XML variable and inserting into table

I am trying to insert some information in Arabic from a XML variable into a SQL Server table.
I did something like the below. However it does not work. Could you please tell what is missing?
Create Table EmployeeTbl
(
firstname nvarchar(100),
familyName nvarchar(100)
)
Insert into EmployeeTbl (firstName, FamilyName)
select
t.x.value('(./firstname)[1]', 'nvarchar(100)') as firstname,
t.x.value('(./familyname)[1]', 'nvarchar(100)') as familyname
from
#XMLVariable.nodes('//employeexml) t(x)
Yes, You missed a single-quotes(') at last line:
Create Table EmployeeTbl
(
firstname nvarchar(100),
familyName nvarchar(100)
)
Insert into EmployeeTbl (firstName, FamilyName)
select
t.x.value('(./firstname)[1]', 'nvarchar(100)') as firstname,
t.x.value('(./familyname)[1]', 'nvarchar(100)') as familyname
from
#XMLVariable.nodes('//employeexml') t(x)
Your XML file should be like that :
<employeexml Id="1">
<firstname>بشیر</firstname>
<familyname>المنادی</familyname>
</employeexml>
<employeexml Id="2">
<firstname>یعغوب</firstname>
<familyname>سمیر</familyname>
</employeexml>

How to show specific 2 rows at the top without order and then sort the rest by order using SQL DISTINCT, UNION

I have a country table with Id, Name with duplicate records. I am Using DISTINCT and UNION to show particular 2 country names Ireland and England at the top and rest all after By order name like below in SQL server
create table country (id int , name varchar(50));
insert into country (id, name) values( 1, 'Kuwait');
insert into country (id, name) values( 2, 'Australia');
insert into country (id, name) values( 3, 'Canada');
insert into country (id, name) values( 4, 'spain');
insert into country (id, name) values( 5, 'Turkey');
insert into country (id, name) values( 6, 'Ireland');
insert into country (id, name) values( 7, 'England');
insert into country (id, name) values( 1, 'Kuwait');
insert into country (id, name) values( 2, 'Australia');
insert into country (id, name) values( 3, 'Canada');
insert into country (id, name) values( 4, 'spain');
insert into country (id, name) values( 5, 'Turkey');
insert into country (id, name) values( 6, 'Ireland');
insert into country (id, name) values( 7, 'England');
DESIRED OUTPUT:
id name
-----------
6 Ireland
7 England
2 Australia
3 Canada
1 Kuwait
4 Spain
5 Turkey
Tried with DISTINCT & UNION in SQL QUERY:but names are not ordered after 3 row
select distinct id,name from country where id in (6,7)
union
select distinct id,name from country where id not in (6,7)
id name
-----------
6 Ireland
7 England
1 Kuwait
2 Australia
3 Canada
4 Spain
5 Turkey `
also tried with ORDER BY CASE:
select distinct id,name from country where id in (6,7)
union
select distinct id,name from country where id not in (6,7)
ORDER BY CASE
WHEN id = 6 and id = 7 -- whatever identifies that row
THEN 1 ELSE 2 END
ERROR:ORDER BY items must appear in the select list if the statement contains a UNION, INTERSECT or EXCEPT operator.
There is absolutely no guaranteed order of the returned result, unless order by is specified. Even if you find a query, that returned the rows in the desired order, on next run, tomorrow, or at client's site, it will not return them in the same order. So you must add an order by clause. You need to sort on some artificial values, like this:
declare #country table(id int , name varchar(50));
insert into #country (id, name) values( 1, 'Kuwait');
insert into #country (id, name) values( 2, 'Australia');
insert into #country (id, name) values( 3, 'Canada');
insert into #country (id, name) values( 4, 'spain');
insert into #country (id, name) values( 5, 'Turkey');
insert into #country (id, name) values( 6, 'Ireland');
insert into #country (id, name) values( 7, 'England');
insert into #country (id, name) values( 1, 'Kuwait');
insert into #country (id, name) values( 2, 'Australia');
insert into #country (id, name) values( 3, 'Canada');
insert into #country (id, name) values( 4, 'spain');
insert into #country (id, name) values( 5, 'Turkey');
insert into #country (id, name) values( 6, 'Ireland');
insert into #country (id, name) values( 7, 'England');
select id, name from (
select distinct id, name
from #country) t
order by case name when 'Ireland' then 'aaaaaaaaaaaaaaaaaaaaaaaa1' when 'England' then 'aaaaaaaaaaaaaaaaaaaaaaaa2' else name end

Need comma separated value in table in SQL Server 2014 [duplicate]

This question already has answers here:
get a comma delimited string from rows [duplicate]
(2 answers)
Closed 4 years ago.
I have a table like this.
Create table #temp
(
id int,
firstname varchar(50),
lastname varchar(50)
)
insert into #temp (id, firstname, lastname)
select 1,'mit','jain'
insert into #temp (id, firstname, lastname)
select 1,'mit','jain1'
insert into #temp (id, firstname, lastname)
select 1,'mit','jain2'
insert into #temp (id, firstname, lastname)
select 2,'mit','jain3'
insert into #temp (id, firstname, lastname)
select 2,'mit','jain4'
insert into #temp (id, firstname, lastname)
select 1,'mit','jain5'
insert into #temp (id, firstname, lastname)
select 1,'mit','jain6'
I want the table to be shown as below
id firstname lastname
----------------------------------------------
1 mit jain,jain1,jain2,jain5,jain6
2 mit jain2,jain4
I have tried the query as below
select
id, firstname,
substring((Select ', '+tc1.lastname AS [text()]
From #temp tc1
Inner Join #temp c1 On c1.id = tc1.id
Where tc1.firstname = c1.firstname
Order BY tc1.lastname
For Xml Path('')), 2, 1000) 'LastName1'
from #temp
group by id, firstname
But it's not working. Please help me out
You're part of the way there. The tradition method is using STUFF:
SELECT t.id, t.firstname,
STUFF((SELECT ', ' + sq.lastname
FROM #temp sq
WHERE sq.id = t.id
AND sq.firstname = t.firstname
ORDER BY sq.lastname
FOR XML PATH('')),1,1,'') AS lastname
FROM #temp t
GROUP BY t.id, t.firstname;
There are lots of answers on SO already on how to do this though, but you have shown effort. :)

MS SQL insert into two tables

I have four tables.
Person(
GUID uniqueidentifier
LastName varchar(20)
FirstName varchar(20)
SSN varchar(20)
ResidenceAddressGUID uniqueidentifier
)
Adress(
GUID uniqueidentifier
Street varchar(50)
Zip varchar(10)
)
Code(
CodeNumber
PersonGUID
Street
Zip
)
Tmp(
FirstName
LastName
Street
Zip
CodeNumber
)
And I want to move the data from Tmp to Person and Adress. What is the simplest way to do it?
EDIT:
I actually found out that TMP has a field "Code" that should be copied to the Code table with the street-adress and zip, no relationship to the address table.
No, you can't insert into multiple tables in one MySQL command. You can however use transactions.
BEGIN
INSERT INTO PERSON(LastName, FirstName)
SELECT
FirstName,
LastName
FROM TMP
INSERT INTO ADDRESS(Street)
SELECT
Street
FROM TMP;
COMMIT;
You can use a INSERT INTO statement combined with a SELECT statement to map the columns from TMP you want to insert into ADDRESS.
INSERT INTO Address(Street, Zip)
SELECT
Street,
Zip
FROM TMP
Also, you do the same thing for PERSON table.
INSERT INTO Person(LastName, FirstName, ResidenceAddressGUID)
SELECT
T.Street,
T.Zip,
A.GUID
FROM TMP T INNER JOIN ADDRESS A
ON T.Street = A.Street
AND T.Zip = A.Zip
Based on your later comments, I modified my queries and I hope now you understand the pattern and can do the queries for the CODE table yourself.
If (Address&Zip) combination is unique, you could use this:
INSERT INTO Address
SELECT DISTINCT
T.Street,
T.Zip
FROM
TMP T LEFT JOIN
Address A ON A.Zip = T.Zip AND Z.Street = T.Street
WHERE
A.GUID IS NULL
INSERT INTO Person (
FirstName,
LastName,
ResidenceAddressGUID)
SELECT
T.FirstName,
T.LastName,
A.GUID
FROM
TMP T INNER JOIN
Address A ON A.Street = T.Street AND A.ZIP = T.ZIP
If they are not unique, you should have a look at mssql INSERT OUTPUT option
Select statements can be used in combination with an insert statement to copy data from one table to another. One caveat, you must include a primary key for the tmp data your moving. In my example, I have included a sequence, which would work with an Oracle database. Depending on how the keys are assigned you may be able to omit the sequence, such as when a MySql autonumber exists.
insert into Person(GUID, FirstName, LastName)
select PERSON_SEQ.nextval, FirstName, LastName from Tmp;
insert into Adress(GUID, Street, Zip)
select ADRESS_SEQ.nextval, Street,Zip from Tmp;

Resources