How can i add column with same name in sqlite - python-3.x

I need to add columns to a database and every second column does not have a name, i wanted to give it a generic "x" name but i get the sqlite3.OperationalError: duplicate column name: x error,
for meci in ech1:
c.execute("ALTER TABLE Aranjate ADD COLUMN "+ ech1[ii] +" INT")
c.execute("ALTER TABLE Aranjate ADD COLUMN x INT")
c.execute("ALTER TABLE Aranjate ADD COLUMN "+ ech2[ii] +" INT")
conn.commit()
ii = ii +1
and i tried to replace x with x = str(ii) so it will not have the same name and insert it as variable:
c.execute("ALTER TABLE Aranjate ADD COLUMN " + x + " INT")
but i suppose that sqlite does not accept integers as table names as i get the error sqlite3.OperationalError: near "0": syntax error where 0 is the first x
It will not be a problem for me if those columns are named the same as all i will do with this table is export it as a csv file

SQLite does not allow column names to be the same because then that would defeat the purpose of a database, you would have contradicting data of the same property for each row.
SQLite also does not allow column names to start with digits, so you can't have 0one as column name, but zero1 is fine. However, you can add brackets around it to make it valid.
Try:
c.execute("ALTER TABLE Aranjate ADD COLUMN [" + x + "] INT")

Related

SQL - Insert a row into table only if not exists and count is less than a threshold

Other similar questions deal these two problems separately, but I want to merge them in a single statement. I'm using Python3 with psycopg2 library on a PostgreSQL database.
I have a table with 3 fields: ID, name, bool (BIT)
I want to insert a row in this table only if does not exists an other row with same 'name' and 'bool' = 0 and the total count of rows with same ID is less than a given threshold.
More specific my table should contain at most a given threshold number of rows with same ID. Those rows can have the same 'name', but only one of those rows with same ID and same 'name' can have 'bool'= 0.
I tried with this:
INSERT INTO table
SELECT 12345, abcdf , 0 FROM table
WHERE NOT EXISTS(
SELECT * FROM table
WHERE ID = 12345 AND name = abcdf AND bool = 0)
HAVING (SELECT count(*) FROM table
WHERE ID = 12345) < threshold
RETURNING ID;
but the row is inserted anyway.
Then I tried the same statement replacing 'HAVING' with 'AND', but it insert all the threshold rows together.

my dataframe contains '/' in column name , How can i plot certain column values in that column name?

i need to plot the state called 'kerala' in the column 'state/unionterritory' and 'confirmed' to create a lineplot.
so far I have written till
1sns.lineplot(x=my_data['state/unionterritory'],y=my_data['confirmed'])
[https://www.kaggle.com/essentialguy/exercise-final-project]
this is the dataframe.head() , see the column name
I assume you want to get a column state/unionterritory from your DataFrame and filter it so it contains only Kerala state:
my_data_kerala = my_data[my_data['state/unionterritory'] == 'Kerala']['state/unionterritory']

How to express current row and current row +1 in Spark SQL

I have a window sql that want to sum the current row with the next, so I write the following sql:
select depName, empNo, salary, sum(salary) over (partition by depName order by empNo rows between CURRENT ROW AND CURRENT ROW + 1) sum_salary from t
But there is grammar error in it,
org.apache.spark.sql.catalyst.parser.ParseException:
missing ')' at '+'(line 2, pos 136)
== SQL ==
select depName, empNo, salary, sum(salary) over (partition by depName order by empNo rows between CURRENT ROW AND CURRENT ROW + 1) sum_salary from t
----------------------------------------------------------------------------------------------------------------------------------------^^^
The correct syntax to define row-based frame is
ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING
and similarly
ROWS BETWEEN 1 PRECEDING AND CURRENT ROW
You can also replace numeric constant with UNBOUNDED keyword.

How to insert values from numpy into sql database with given columns?

I need to insert some columns into a table in my Mariadb. The table name is Customer and has 6 columns, A,B,C,D,E,F. The primary keys are in the first column, column B has an address, C,D, and E contain None values and F the zip code.
I have an pandas dataframe that follows similar format. I converted it to numpy array by doing the following:
data = df.iloc[:,1:4].values
hence data is a numpy array containing 3 columns and i need this inserted into C,D and E. I tried:
query = """
Insert Into Customer (C,D,E) VALUES (?,?,?)
"""
cur.executemany(query,data)
cur.commit()
But i get an error:
The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I solved it. Although very slow...
query = """
Alter Customer SET
C = %s
D = %s
E = %s
where A = %s
"""
for row in data:
cur.execute(query,args=(row[1],row[2],row[3],row[0])
con.commit()

SQL Server PIVOT - Vertical to Horizontal - error

Will you please help to check this?
CREATE TABLE Sales
(
Category NVARCHAR(30),
Region NVARCHAR(30),
Amount MONEY
)
DELETE FROM Sales
INSERT INTO Sales
VALUES
('X','1',24),
('X','2',NULL),
('X','3',165),
('X','4',36),
('Y','1',38),
('Y','2',181),
('Y','3',287),
('Y','4',NULL),
('Z','1',83),
('Z','2',55),
('Z','3',33),
('Z','4',44)
DECLARE #SQLStr NVARCHAR(MAX)
SELECT #SQLStr = COALESCE(#SQLStr + ',' ,'')+ [a].[Column]
FROM (SELECT DISTINCT Region AS [Column]
FROM Sales) AS a
SET #SQLStr = 'SELECT Category, ' + #SQLStr + ' FROM (SELECT Category, Region, Amount FROM Sales) sq '
+ ' PIVOT (SUM(Amount) FOR Region IN (' + #SQLStr + ')) AS pt'
PRINT #SQLStr
EXEC sp_executesql #SQLStr
I get an error
Msg 102, Level 15, State 1, Line 35
Incorrect syntax near '1'.
Initially, the column 'Region' is INT type (values are 1, 2, 3, 4)
The same error, then I change its type to NVARCHAR(30), but the same error happens. Please help.
In reality, I want to transpose the vertical product usage data to horizontal.
Please see the image link
Please see this screen shot, this is what I really want to achieve
As you see, the date is transposed to horizontal. I want to populate the usage data to horizontal way for my analysis. Please help
The column header can be date? or have to be string?
please add brackets with column name like below
DECLARE #SQLStr NVARCHAR(MAX)
SELECT #SQLStr = COALESCE(#SQLStr + ',' ,'')+ +'['+[a].[Column]+']'
FROM (SELECT DISTINCT Region AS [Column]
FROM #Sales) AS a
You should always place square brackets around the column names []:
From MSDN:
The first character must be one of the following:
A letter as defined by the Unicode Standard 3.2. The Unicode definition of letters includes Latin characters from a through z, from A through Z, and also letter characters from other languages.
The underscore (_), at sign (#), or number sign (#).
If it is anything else, then you will need to use the square brackets. []

Resources