How can i add multiple elements for a single column in a row:
Say i have a column, topic, which can have infinitely many elements inside:
topics = ['Particle Physics,'Karaoke','jazz']
I have a statement in sqlite:
def UpdateElement(new_user,new_topic):
new_topic = new_topic + "; "
querycurs.execute('''UPDATE First_Data SET topic = (?) WHERE user = (?)''', (new_topic, new_user))
However this will allow only one element at a time to exist under the topic column. How can you edit the code so that it can add another given element to the current topic.
If in the table topic = ['Math'], then i could make it into topic = '[Math; Python']. This way i can use simple python .join statement to split it.
With a text field you can store anything. You could store the list as semi-colon delimited string or as a json object string. You could also pickle the list and store it as a base64 string. The problem with all of these solutions is that you lose a level of access to your data. To count how many users have like Jazz topic, you need to read/split the text field...or use some more complicated LIKE statement.
Since you are using SQL, you may want to consider normalizing your data to include a Topic table, a User table, and a cross-walk table with foreign keys to your users and topics to enforce the many-to-many relationship. While its a bit more to setup, but it can be simpler to update when user topics change.
Related
I have a function written in nodejs and another in python. They both do the same thing in different scripts.
I currently have a function that creates a firestore collection called profile, then insert a document, which has as a name, the document id created by firestore
The document itself contains an object, representing the user, first name, last name, email, and phone.
the phone is a list, or array [], it always contains just one phone number, later on we'll add multiple phone numbers per user but for now, you can assume that there is just one element in a list. Phone can never be null, email can be null sometimes.
What I want to do, is if the phone doesn't exist already then insert a new user, otherwise update the existing user. So there should be no duplication.
Can this be done via merge in firestore or update or should I do a where query? I know that a query is always possible, but I want to know if I can use a merge on a list field in firestore.
If you don't have any document reference to update to and need to query the field phone_number then you need to use the where() method with the array_contains operator. Use the array_contains operator to filter based on array values. If a document is found you can use arrayUnion() to add elements to an array but only elements are not already present. See sample snippets below:
Python:
# Collection Reference
col_ref = db.collection(u'profile')
# Check collection if there's any array in documents that contains the `phone_number`
admin_ref = col_ref.where(u'phone_number', u'array_contains', 12345).get()
# Checks if there's a result.
if admin_ref:
# Iterate the result
for item in admin_ref:
doc = col_ref.document(item.id)
doc.update(
# Adds multiple phone numbers to the field, but only adds new ones.
{u'phone_number': firestore.ArrayUnion([12345,54321])})
# If there's no result, then add a document with the field name `phone_number`
else:
col_ref.add({'phone_number': [123456]})
For more information, you can check these documentations:
Array membership
Update elements in an array
How to use python to input a line of query string (you don’t know how many fields there are, and you don’t know whether there are repeated field names) and output the query string without repeated fields (the order of the fields does not change)?
Example:
Input: username=peter&score=80&username=john
Output: username=john&score=80
A simple solution will be like
query = "username=peter&score=80&username=john"
unique = {f[0]:f[1] for f in [field.split("=") for field in query.split("&")}]}
output= "&".join([f"{field}={value}" for field, value in unique.items()])
But, to be precise, this is a happy path solution. If the input formats has issues, this will fail. Its ideal to use a proper library to handle the query string. The uniqueness of fields are determined with the key alone here.
I'm having an issue here that I want clarification with, see, I'm making a program that does analysis of data.
I wanted to query data from different users, the data is numerical by the way, whenever I'd get subject marks from a user, I want the system to return the name of the user who has those marks.
It was all working fine until I tried querying the users with the same marks, and all I could get was an error
analyzer.models.ResultsALevel.MultipleObjectsReturned: get() returned more than one
ResultsALevel -- it returned 4!
So I was trying to find a way to still query and return the name of users with that subject mark, even if they have the same marks.
I believe that should be possible since the users have different id's and stuff, help would be much appreciated!
Here's my views.py
biology_marks = []
for student in ResultsALevel.objects.all():
biology_marks.append(student.advanced_biology_1)
value_1_biology =
ResultsALevel.objects.get(advanced_biology_1=biology_marks[-1]).student_name.last_name
value_2_biology =
ResultsALevel.objects.get(advanced_biology_1=biology_marks[-2]).student_name.last_name
value_3_biology =
ResultsALevel.objects.get(advanced_biology_1=biology_marks[-3]).student_name.last_name
value_4_biology =
ResultsALevel.objects.get(advanced_biology_1=biology_marks[-4]).student_name.last_name
ObjectManager.get() is used to retrieve single instances, usually selected by a primary key field.
Using ObjectManager.get() on a field where multiple data exists which matches the query, an error is returned (MultipeObjectsReturned)
Use ObjectManager.filter() in stead.
I am using qpython to query into a KDB+ database and then performing operations on the output. old_df is output from an earlier qpython sync query which has '[source_id]' as a string column. Now am querying into another database trades_database which has the same fields (as source_id) under a different column name customer (also string, no issues in data type)
params = np.array([])
for i in old_df['source_id']:
params = np.append(params, np.string_(i))
new_df = q.sync('{[w]select from trade_database where customer in w}', *params, pandas=True)
Unfortunately, there is very little available online to solve such queries. I have learned a fair bit from the questions asked in here, but am really stuck here. My list could be very long and so would need to write a query where it is taken as an input only.
I also tried:
new_df= q1.sync('{select from trades_database where customer in (`1234, `ABCD)}', pandas=True)
which works but I get
<qpython.qtype.QLambda object at 0x000000000413F710>
How does one "unpack" a QLambda object?
Please ignore the 2nd question if I am not allowed to ask 2 questions in the same post pls. Apologies in that case.
Thanks!
here is what I did and it seems to work:
params = np.array(one_id) #just input the initial id used to search for old_df, and not put the square brackets to make it into a list
for i in old_df['source_id']:
params = np.append(params,np.string_(i))
params=np.unique(params)
new_df = q1.sync('{[w]select from trades_database where customer in w}', params, pandas=True)
Here's what I am trying achieve but not sure how to make it happen.
I am building a literature analytic tool. User will paste some text into a field, the system will split word by word into a database table one row for each word, and they have to be sequential. By doing so, later then we can edit and define the translation/annotation of each word.
I am new to Django and hvn't figure out the whole logic behind it yet, but I am curious if it is achievable.
First things first, from the tutorial I just been through from the official Django website, every field created equals to a "cell" in a database table, so what happens if I want to split the content in the input field and store them rows of data in a database table with an ID incremented to index the order of them?
Second, if the above is achieved, when i need to add a word(record) in the middle of the database, like adding something in the middle of an array, is it achievable?
Not sure if what you're asking but this is how I would do it based on what you have said
# models
class Word(models.Model):
word = models.CharField("""field args""")
# Views
words = form.cleaned_data['sentance'].split(" ")
for word in words:
_ = Word.objects.create(word=word)