py2neo update node in graph - database

i started using the py2neo database system with django.
How can i update a node in the graph database ?
I created a node:
user = graph_db.get_or_create_indexed_node("users_email_single", email, email,
{"user_id":user_id,
"basic_name_firstname": firstname,
"basic_name_lastname": lastname,
"contactprivate_email": email,
})
I get the node with following code:
graph_db = neo4j.GraphDatabaseService("http://localhost:7474/db/data/")
user = graph_db.get_indexed_node("users_user_id", user_id, user_id)
Regards

You can also set individual properties outside a batch as follows:
user["name"] = "Bob"
user["age"] = 77

Related

Parameter count mismatch SQLTE

Fairly newbie here to this and need some help, trying to save new employee information into my sqlite database from a user management screen in my application. I am throwing a Parameter count mismatch when I try to save new employee data into my sqlite employeeinfo database.
columns in my sqlite database table are as follows; Registration,Name,Surname,Access,Phone,Email,Username,Password
I am using the following code on pushbutton clicked;
void oviewsettings::on_pushButton_clicked()
{
QString Registration, Name, Surname, Access, Phone, Email, Username, Password;
Registration=ui->lineEdit_usermanagement_Registration->text();
Name=ui->lineEdit_usermanagement_Name->text();
Surname=ui->lineEdit_usermanagement_Surname->text();
Access=ui->lineEdit_usermanagement_Access->text();
Phone=ui->lineEdit_usermanagement_Phone->text();
Email=ui->lineEdit_usermanagement_Email->text();
Username=ui->lineEdit_usermanagement_Username->text();
Password=ui->lineEdit_usermanagement_Password->text();
OViewMain conn;
if(!conn.connOpen()){
qDebug()<<"Failed to open the database";
return;
}
conn.connOpen();
QSqlQuery qry;
qry.prepare("INSERT INTO employeeinfo(Registration,Name,Surname,Access,Phone,Email,Username,Password) VALUES ('"+Registration+"','"+Name+"','"+Surname+"','"+Access+"','"+Phone+"','"+Email+"','"+Username+"','"+Password+"')");
qry.addBindValue(":Registration");
qry.addBindValue(":Name");
qry.addBindValue(":Surname");
qry.addBindValue(":Access");
qry.addBindValue(":Phone");
qry.addBindValue(":Email");
qry.addBindValue(":Username");
qry.addBindValue(":Password");
qry.exec();
if(qry.exec())
{
QMessageBox::critical(this,tr("Save"),tr("Database Updated, Saved"));
conn.connClose();
}
else
{
QMessageBox::critical(this,tr("Error"),qry.lastError().text());
}
}
Not familiar with qt but from using other prepared statements and looking at the manual it seems like it should be the following code.
qry.prepare("INSERT INTO employeeinfo(Registration, Name, Surname, Access, Phone, Email, Username, Password) VALUES (:Registration, :Name, :Surname, :Access, :Phone, :Email, :Username, :Password)");
qry.BindValue(":Registration", Registration);
qry.BindValue(":Name", Name);
qry.BindValue(":Surname", Surname);
qry.BindValue(":Access", Access);
qry.BindValue(":Phone", Phone);
qry.BindValue(":Email", Email);
qry.BindValue(":Username", Username);
qry.BindValue(":Password", Password);
qry.exec();
The placeholders need to be in the query so the driver knowns where to write the values. The binding then needs a mapping to the placeholder and the value it should have. Additionally BindValue() uses named placeholders, addBindValue() uses anonymous/unnamed placeholders, here's an example using the latter.
qry.prepare("INSERT INTO employeeinfo(Registration, Name, Surname, Access, Phone, Email, Username, Password) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
qry.addBindValue(Registration);
qry.addBindValue(Name);
qry.addBindValue(Surname);
qry.addBindValue(Access);
qry.addBindValue(Phone);
qry.addBindValue(Email);
qry.addBindValue(Username);
qry.addBindValue(Password);
qry.exec();
Thanks for the above, that makes total sense now you have broken it down.
Your updated code is on the money just with a small typo the bindValue has a capital B in your code and QT is case sensitive. so have retyped it below for anyone else having this issue to see it.
One further note: Although with the updated code it saves the user into the sql database, it throws a new error: UNIQUE constraint failed: employeeinfo.Registration Unable to fetch row.
But it does save the user into the DB. I would suppose I have to create a new question to why it throws this error, but would love it if someone could answer that to complete this thread.
See updated code here:
QSqlQuery qry;
qry.prepare("INSERT INTO employeeinfo(Registration, Name, Surname, Access, Phone, Email, Username, Password) VALUES (:Registration, :Name, :Surname, :Access, :Phone, :Email, :Username, :Password)");
qry.bindValue(":Registration", Registration);
qry.bindValue(":Name", Name);
qry.bindValue(":Surname", Surname);
qry.bindValue(":Access", Access);
qry.bindValue(":Phone", Phone);
qry.bindValue(":Email", Email);
qry.bindValue(":Username", Username);
qry.bindValue(":Password", Password);
qry.exec();

I need to get the user id from my users table in DynamoDB?

I'm using AWS Amplify on my react native app
I want to do something seemingly so simple, but i am a bit lost as to how to do it: I have a table with user information already in it. Here's the Schema:
type Users #model {
id: ID!
userName: String
firstname: String
weblink: String
email: String
mobileNum: String
.
.
.
}
//Here's my Query.js
export const getUsers = `query GetUsers($id: ID!) {
getUsers(id: $id) {
id
userName
firstname
weblink
email
.
.
.
}
}
`;
This table is populated in DynamoDB when i check my AWS console. What i need is to be able to get the id from the table using the userName (not vice versa). The id is generated when i createUser() and it's used throughout my app to get all my user's information. However when a user signs in on a new phone, this id isn't available anymore. So when they sign in via Cognito, i do know the userName and all i need to do is retrieve this id.
Normally i would do this, but obviously i want to do the reverse and use my unique userName to get the id:
const userData = await API.graphql(graphqlOperation(GetUsers, { id: usersId })); //usersId = "3b0dae-24j5-4401-95a6-11seyhaf1g131"
Any idea how i can get my users id?
Assuming that the userName is unique, create a Global Secondary Index with userName as the partition key. Project, at least, the id attribute into the index. Finally, search that index instead of the underlying table.
Alternatively, if you never need to lookup by id, consider making userName the partition key and id a regular attribute.

How can I assigned roles automatically in webmatrix

I want to assigned new registered users automatically the role of Member in Database.
WebMatrix sample Starter Site app - Account\Register.cshtml
Tested and it works.
// Check if user already exists
var user = db.QuerySingle("SELECT Email FROM UserProfile WHERE LOWER(Email) = LOWER(#0)", email);
if (user == null) {
// Insert email into the profile table
db.Execute("INSERT INTO UserProfile (Email) VALUES (#0)", email);
//Roles have already been added to webpages_Roles table
//Logic to determine role for e-mail (user) being added for the first time
if (email == "John#gmail.com") {
var userName=email;
var roleName="Administrator";
Roles.AddUserToRole(userName, roleName);
}
else if (email == "Greg#gmail.com") {
var userName=email;
var roleName="Guest";
Roles.AddUserToRole(userName, roleName);
}
else {
//All others are assigned the role of Member
var username=email;
var roleName="Member";
Roles.AddUserToRole(userName, roleName);
}
In your register code, which in the sample WebMatrix app is found in the file Account\Register.cshtml, there's if statements that first determine that the information being submitted is valid and then that the email doesn't exist in your database. Within those if statements, if the user is successfully created, then you can add code like:
// boy, is this a hack, Member = RoleID 8
db.Execute("INSERT INTO webpages_UsersInRoles (UserId, RoleID) VALUES( #0, 8 )", UserID);
Now I used 8 as an example. Look in your webpages_Roles table. This has a list of RoleNames and their RoleID. If you've already created the Member role, it will be in this table. Use the corresponding RoleID instead of the 8 that I used.

Get the ID in a child entity of NDB GAE Datastore

I'm going in circles on getting the id of NDB Datastore.
I have setup the webapp2.RequestHandler to catch the email and get the ID. Basically my goal is to delete an entity, but if I pass the email address to get the ID of the entity, I'm stump, because it gives me results I was just getting. I used ID instead of key_name.
I tried finding the ID by querying via email, but it seems like using query does not have a method attribute to find the id.
def get(self,email):
user = users.get_current_user()
if user:
user_key = ndb.Key('UserPrefs',user.email())
contacts = Contact.query(Contact.email==email,ancestor=user_key)
self.response.write(contacts.id) # there is no attribute such as Contact.id
I tried to find the ID by getting the key, but when I displayed the key, it showed me whatever value I have in the email variable
def get(self,email):
user = users.get_current_user()
if user:
user_key = ndb.Key('UserPrefs',user.email())
contact_key = ndb.Key('Contact',email,parent=user_key)
self.response.write(contact_key.id())
Real Question: So, given that I do not have the ID, how do I find the correct ID inside an entity if I saved my entities via id and not key_name?
Here are the mixture of codes that I'm trying out.
def get(self,email):
user = users.get_current_user()
if user:
user_key = ndb.Key('UserPrefs',user.email())
contact_key = ndb.Key('Contact',email,parent=user_key)
contacts = Contact.query(Contact.email==email,ancestor=user_key)
contact = contacts.get()
contact_key.delete()
# self.response.write(contact.name) # this works
self.response.write(contact_key.id()) # this does not work because I do not have the entity id, and I'd like to get it blindfolded. Is there a way?
Here is my Model for Contact.
class Contact(ndb.Model):
name = ndb.StringProperty()
phone = ndb.StringProperty()
email = ndb.StringProperty()
dateCreated = ndb.DateTimeProperty(auto_now_add=True)
dateUpdated = ndb.DateTimeProperty(auto_now=True)
The docs state:
The identifier may be either a key "name" string assigned by the application or an integer numeric ID generated automatically by the Datastore.
Since you are defining the name property on your Contact class, this is used as the identifier. (You don't want that because in real world different users can have same names)
So if you want NDB to generate numeric IDs for your entities, rename the name property to something else, e.g. username.
Update: let's go step by step:
Problem with the first example is that you are trying to get id on the Query. Query class has no id property defined on it. You should call get() on it:
# get() or fetch() should be called on query to return some data
contacts = Contact.query(Contact.email==email,ancestor=user_key).get()
self.response.write(contacts.id) # there is no attribute such as Contact.id
Problem with the second piece of code is that you are just initialising a Key and providing email as id - the second param of constructor is the id and you are providing email as value. Hence you are getting the email out. Also, there is no database operation here.
Note: the identifiers, which are id, key, urlsafe, or value (for the query) should be passed from the HTTP Request by webapp2.RequestHandler from a parsed url or HTTP POST, GET, PUT, or DELETE.
If you do not have any identifiers or values passed from an HTTP request, it could be difficult to access the specific entity (or the record). So, it is important to take note to pass a form of identifier or value to access the specific entity (or the record in database terms).
So, you can do the following to get the id:
Access by value:
def get(self,email):
user = users.get_current_user()
if user:
user_key = ndb.Key('UserPrefs',user.email())
contacts = Contact.query(Contact.email==email,ancestor=user_key)
contact = contacts.get()
id = contact.key.id() # this access the entity id directly because you have the data.
self.response.write(id)
Access by urlsafe:
def get(self,urlString):
user = users.get_current_user()
if user:
contact_key = ndb.Key(urlsafe=urlString) #urlString refers to the key of contact
contact = contact_key.get()
id = contact.key.id() # this access the entity id directly because you have the data.
self.response.write(id)
Access by HTTP POST Request:
def post(self):
user = users.get_current_user()
if user:
user_key = ndb.Key('UserPrefs',user.email())
email = self.request.get('email')
contacts = Contact.query(Contact.email==email,ancestor=user_key)
contact = contacts.get()
id = contact.key.id() # this access the entity id directly because you have the data.
self.response.write(id)

Can't select User ID from Salesforce

I'm trying to select user's subordinates from Salesforce, but a simple query
SELECT Id FROM User WHERE ManagerId=xxxxxxxxx
returns bunch of null values, when I run
SELECT Id,Name FROM User WHERE ManagerId=xxxxxxxx
I get the correct names, still no IDs.
Is this a permission issue? I can't find anything when I login to portal.
I'm running the queries via API on Sandbox environment.
Try this (both works for me allways):
Id myId = [Select Id From User Where Username = 'myUserName'].Id;
System.debug('#### myId: ' + myId);
List<User> myIdList = [Select Id From User Where Username = 'myUserName' Limit 1];
System.debug('#### myId from list: ' + myIdList[0].Id);
Portal Licence doesn't allow to query User. However you have still access to the name of the user through OwnerId, CreatedById, LastModifiedById using in an inputfield.
i.e :
If you want to have access to user through the portal you need a custom object and synchronise your records with User by trigger.

Resources