Is there a way to insert data to a user made column in for SQLite? - python-3.x

I am working on a Python program that takes in user input and stores the inputs to a database using SQLite. A problem that I run into is that I want to give the option to the user to make a new column in the table, but I don't know how to insert/edit/delete/search data to the new column. Is there a good way to do this in Python? My first idea is to make a list of all the column names and make a separate list to check which columns the user wants to insert/search to and do the query from there, but that does not seem to be a good idea to me because it makes the user experience not good since it basically asks the user what to input every time the user wants to input something. Please help. Thank you.
Edit: This is an example scenario:
Say, a table has 4 columns: name, address, date of birth, and job. There are functions that ask for user input for those columns and store it to the database. Then, the user adds another column, say phone number. How can you insert/search data to/from the phone number column?

You can add a new column to your table using :
ALTER TABLE useTable ADD COLUMN phone TEXT DEFAULT '-';
-- Insert Data
INSERT INTO userTable (name,address,dateBirth, phone)
VALUES(searchedName,searchedAddress,searcheDateBirth, searchedPhone);
-- Search for Data, eg phone
SELECT * from userTable where name like '%Sky%'
see doc for restictions

Related

How to enter variable number of values in variable no of columns defined by user in python using sqlite?

I am doing an ML project of creating a database using voice input.
I am taking the no of columns the user wants and the column names by voice input.
I am storing the column names in a list.
Now how can I create a table with User entered name, user entered no of columns and column names?
colList=[]
def enterData():
print('How many columns do you want in your Database?')
n=int(SpeechToText())
print(n,' Columns?')
ch1=SpeechToText()
print(ch1)
if ch1=='yes' or ch1=='YES':
for i in range(n):
print('Enter column',i+1)
col=SpeechToText()
colName=col.capitalize()
elif ch1=='no' or ch1=='NO':
enterData()
Human Learning must come before Machine Learning! :)
Familiarize yourself with the python sqlite3 API and keep the sqlite3 doc handy.
import the sqlite3 library
create a database connection
create a query string by iterating through colList=[]. (Assume it's your intention to keep a list of the columns entered even though the code to do so is not there)
execute the query string
You'll probably want to ask the user what to name the table also. NB: columns aren't "in a database", they are "in tables in a database".
To explain #4 in more detail:
Remember, the sql argument of the execute method is simply a string. And since the column list in a sqlite CREATE statement looks a lot like a python tuple, you can do it without an iterator. Something like:
query_string = "CREATE TABLE tname " + str(tuple(colList))" will create a string that can be passed to the execute method to create the table.

Displaying SQL data from multiple tables

I have two tables that hold information needed to display time clock interaction in an excel sheet. The data will need to update with every time clock interaction. I joined the two tables and it was pointed out to me that data duplication is a big no no. Looking for a more simple solution than to do a join everyday so I can have recent interactions. Once I can get the SQL end set up, I can handle the excel side.
Table info:
From the dbo.employees table I need the ID, Last_Name, First_Name
From the dbo.employeetimecardactions I need ID, ActionTime, ActionDate, ShiftStart, Action Type.
ID is the common column between the two tables of course.
If my JOIN statement is needed I will supply, but seeing as the data duplication is a problem I would like to start fresh with NO prior code brought into it.
Also any additional information needed can be supplied if I know exactly what is needed
END RESULT- Excel File that I can share with the powers that be. Contains all recent time clock interactions. Also it would be nice to be able to search by date or employee but that should be an Excel function I would think, and not absolutely necessary
Please check the names of the two tables and correct appropriately, this is based on the first part of this thread and later comments:
SELECT E.EmployeeID, E.First_Name, E.Last_Name, A.ActionTime, A.ActionDate, A.ShiftStart, A.ActionType
FROM Employees E LEFT OUTER JOIN
EmployeeTimeCardActions A ON E.EmployeeID=A.EmployeeID
Here's a WHERE clause to include date. Please check your DB for date format to use:
="WHERE ActionDate BETWEEN '" & TEXT(A2,"mm/dd/yyyy") & "' AND '"&TEXT(B2,"mm/dd/yyyy")&"'"
The formula is in cell C2

An outer join Excel Power Pivot Pivot table?

I have a PowerPivot with two tables one contains a list of facilities, their type (active/inactive) and whether they belong to org A or org B (FaciltyID|Active/Inactive|ORG)
Another table has a list of users and facitilites assigned to them + their org, so it looks like (userID|FacilityID|ORG) where each userID is repeated the number of times that=the number of facilties it has.
Initially I needed to report the number of facilities active and easily built a PivotTable for it.
Now I need to get a list of the facilities that each user is missing , so I need to basically do an outer join between the the tables for each user and I just can't figure out the way to do it! I joined both table on the FacilityID and am able to see whether they have inactive facilties, but can't figure out a way to show all the facilities they are missing!
Thanks
Nonexistence is hard. This is not the sort of thing that is best solved through measures, but through modeling. In your source, you should cross join Facility and User to get FacilityUser. In FacilityUser, every user has 1 row with every facility, and you add a flag to indicate whether the user is or isn't assigned to that facility. Then your problem becomes one of filtering on that flag value. This is solvable in DAX, but you don't want to do that.

How do I create report-like data tables in Excel?

In the past I have created websites that extract data from a database and format it using tables.
Now, I am trying to do the same thing but with Excel, and I'm lost. I am used to using SQL commands to extract data from given fields and then sort/manipulate it.
Currently, I am able to print a report that provides me with an Excel spreadsheet full of raw data, but I would like to make my life easier and organize it into a report.
The column that I would like to reference contains duplicates, but the data in the adjacent columns is different.
To give an example, assume I had a spreadsheet of sales transactions. One column would be the Customer ID, and the adjacent columns would contain the quantity, the cost per unit, total cost, order ID, etc.
What I would want to do in this case would be to select all the transactions with the same Customer ID and add them together based on their Order ID. Then, I would want to print the result to a second sheet.
I realize that I can use built-in functions to accomplish this, but I would also like to format this report evenually using VBA. Also, since I will have a variable number of rows that differ from one report to the next, I haven't encountered a fucnction that will allow you to add rows.
I'm assuming this must be done with VBA.
Well you can do it manually, but it would take ages. So VBA would be good, particularly as you would be able to generate future reports quickly.
My interpretation of what your saying is that each row in your report will be the total for one customer ID. If it's something else, I imagine the below will still be mostly relevant.
I think it would be a bit much to give you the full answer, particularly as you haven't provided full detail but to take a stab at what you'd do:
Create your empty report page, whether it be a new worksheet or a new workbook
Loop through the table (probably using While next is not empty)
a. Identifying if a row is for a customer ID you haven't covered yet
i. If so then add a new entry in your report
ii. Else add it to the existing customer ID record (loop through until you find it)
Format your report so it looks pretty, e.g:
a. Fill the background in white
b. Throw in some filled bars
c. Put in good titles and totals etc.
For part 1, it might be better building an array first and then dumping the contents into the report. It depends how process intensive it will be - if very intense, an array should shave off time.

Lookup value in column to display name

I've got 2 columns - Name ID and Name.
When a user adds a new entry to the sharepoint datasheet in the form, they have a dropdown list of all names (around 20 in total).
What can I do, so that when they select the name, the Name ID field auto-populates with its corresponding details?
Is there somewhere I need to hold this data and how do I go about creating these calculated columns?
Thanks in advance
You should just be able to do something like this
=TEXT([NameIDcolumn])
# Or
=CONCATENATE("You selected:", [NameIDColumn])
However, you cant use the value from the ID column or any think like that in a calculated col since the value does not exist yet. The SharePoint dev team was most likely smoking crack when creating that datatype since a lot of good stuff that should have been there isn't.
You have the complete reference of stuff you can make with calculated columns here http://office.microsoft.com/en-us/windows-sharepoint-services-help/CH010065006.aspx

Resources