Inserting multiple values in django database - raw sqlite statements - sql-server
I use the raw sql in django..
p.execute('''INSERT INTO webapp_information VALUES(login=%s, idd=%s, avatar_url=%s, gravatar_id=%s, url=%s, html_url=%s, followers_url=%s, following_url=%s, gists_url=%s, starred_url=%s, subscriptions_url=%s, organizations_url=%s, repos_url=%s, events_url=%s, received_events_url=%s,typ=%s,site_admin=%s, name=%s, company=%s, blog=%s, location=%s,email=%s, hireable=%s, bio=%s, public_repos=%s, public_gists=%s, followers=%s, following=%s, created_at=%s, updated_at=%s, dat=%s''',i)
i is the list of values
follow the custom sql given in the documentation https://docs.djangoproject.com/en/2.1/topics/db/sql/
/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.pyc in execute(self, sql, params)
77 start = time()
78 try:
---> 79 return super(CursorDebugWrapper, self).execute(sql, params)
80 finally:
81 stop = time()
/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.pyc in execute(self, sql, params)
62 return self.cursor.execute(sql)
63 else:
--> 64 return self.cursor.execute(sql, params)
65
66 def executemany(self, sql, param_list):
/usr/local/lib/python2.7/dist-packages/django/db/utils.pyc in __exit__(self, exc_type, exc_value, traceback)
92 if dj_exc_type not in (DataError, IntegrityError):
93 self.wrapper.errors_occurred = True
---> 94 six.reraise(dj_exc_type, dj_exc_value, traceback)
95
96 def __call__(self, func):
/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.pyc in execute(self, sql, params)
62 return self.cursor.execute(sql)
63 else:
---> 64 return self.cursor.execute(sql, params)
65
66 def executemany(self, sql, param_list):
/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/base.pyc in execute(self, query, params)
335 return Database.Cursor.execute(self, query)
336 query = self.convert_query(query)
--> 337 return Database.Cursor.execute(self, query, params)
338
339 def executemany(self, query, param_list):
OperationalError: near "?": syntax error
i never used the '?' where the django asked to use '%s'
what to do.. thanks
Your i variable must be a list
i=[...]
p variable is a cursor object
p.execute('INSERT OR REPLACE INTO
webapp_information(login, idd, avatar_url, gravatar_id, url, html_url, followers_url, following_url, gists_url, starred_url, subscriptions_url, organizations_url, repos_url, events_url, received_events_url,typ,site_admin, name, company, blog, location, email, hireable, bio, public_repos, public_gists, followers, following, created_at, updated_at, dat) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)',i)
Related
Sagemaker - UnkownServiceError for Session
I am trying to run a simple model in sagemaker. When trying to run the following code I keep getting this error. It is a very simple code that I saw in some tutorials and in sagemaker examples that are included in the Jupiter notebook. Does anyone know what should I do to make it work? imput: import sagemaker sess = sagemaker.Session() output: --------------------------------------------------------------------------- UnknownServiceError Traceback (most recent call last) /tmp/ipykernel_11350/4126940475.py in <cell line: 3>() 1 import sagemaker 2 ----> 3 sess = sagemaker.Session() ~/anaconda3/envs/python3/lib/python3.8/site-packages/sagemaker/session.py in __init__(self, boto_session, sagemaker_client, sagemaker_runtime_client, sagemaker_featurestore_runtime_client, default_bucket, settings, sagemaker_metrics_client) 131 self.settings = settings 132 --> 133 self._initialize( 134 boto_session=boto_session, 135 sagemaker_client=sagemaker_client, ~/anaconda3/envs/python3/lib/python3.8/site-packages/sagemaker/session.py in _initialize(self, boto_session, sagemaker_client, sagemaker_runtime_client, sagemaker_featurestore_runtime_client, sagemaker_metrics_client) 183 self.sagemaker_metrics_client = sagemaker_metrics_client 184 else: --> 185 self.sagemaker_metrics_client = self.boto_session.client("sagemaker-metrics") 186 prepend_user_agent(self.sagemaker_metrics_client) 187 ~/anaconda3/envs/python3/lib/python3.8/site-packages/boto3/session.py in client(self, service_name, region_name, api_version, use_ssl, verify, endpoint_url, aws_access_key_id, aws_secret_access_key, aws_session_token, config) 297 298 """ --> 299 return self._session.create_client( 300 service_name, 301 region_name=region_name, ~/anaconda3/envs/python3/lib/python3.8/site-packages/botocore/session.py in create_client(self, service_name, region_name, api_version, use_ssl, verify, endpoint_url, aws_access_key_id, aws_secret_access_key, aws_session_token, config) 868 retryhandler, translate, response_parser_factory, 869 exceptions_factory, config_store) --> 870 client = client_creator.create_client( 871 service_name=service_name, region_name=region_name, 872 is_secure=use_ssl, endpoint_url=endpoint_url, verify=verify, ~/anaconda3/envs/python3/lib/python3.8/site-packages/botocore/client.py in create_client(self, service_name, region_name, is_secure, endpoint_url, verify, credentials, scoped_config, api_version, client_config) 85 'choose-service-name', service_name=service_name) 86 service_name = first_non_none_response(responses, default=service_name) ---> 87 service_model = self._load_service_model(service_name, api_version) 88 cls = self._create_client_class(service_name, service_model) 89 region_name, client_config = self._normalize_fips_region( ~/anaconda3/envs/python3/lib/python3.8/site-packages/botocore/client.py in _load_service_model(self, service_name, api_version) 152 153 def _load_service_model(self, service_name, api_version=None): --> 154 json_model = self._loader.load_service_model(service_name, 'service-2', 155 api_version=api_version) 156 service_model = ServiceModel(json_model, service_name=service_name) ~/anaconda3/envs/python3/lib/python3.8/site-packages/botocore/loaders.py in _wrapper(self, *args, **kwargs) 130 if key in self._cache: 131 return self._cache[key] --> 132 data = func(self, *args, **kwargs) 133 self._cache[key] = data 134 return data ~/anaconda3/envs/python3/lib/python3.8/site-packages/botocore/loaders.py in load_service_model(self, service_name, type_name, api_version) 375 known_services = self.list_available_services(type_name) 376 if service_name not in known_services: --> 377 raise UnknownServiceError( 378 service_name=service_name, 379 known_service_names=', '.join(sorted(known_services))) UnknownServiceError: Unknown service: 'sagemaker-metrics'. Valid service names are: accessanalyzer, account, acm, acm-pca, alexaforbusiness, amp, amplify, amplifybackend, amplifyuibuilder, apigateway, apigatewaymanagementapi, apigatewayv2, appconfig, appconfigdata, appflow, appintegrations, application-autoscaling, application-insights, applicationcostprofiler, appmesh, apprunner, appstream, appsync, athena, auditmanager, autoscaling, autoscaling-plans, backup, backup-gateway, batch, braket, budgets, ce, chime, chime-sdk-identity, chime-sdk-meetings, chime-sdk-messaging, cloud9, cloudcontrol, clouddirectory, cloudformation, cloudfront, cloudhsm, cloudhsmv2, cloudsearch, cloudsearchdomain, cloudtrail, cloudwatch, codeartifact, codebuild, codecommit, codedeploy, codeguru-reviewer, codeguruprofiler, codepipeline, codestar, codestar-connections, codestar-notifications, cognito-identity, cognito-idp, cognito-sync, comprehend, comprehendmedical, compute-optimizer, config, connect, connect-contact-lens, connectparticipant, cur, customer-profiles, databrew, dataexchange, datapipeline, datasync, dax, detective, devicefarm, devops-guru, directconnect, discovery, dlm, dms, docdb, drs, ds, dynamodb, dynamodbstreams, ebs, ec2, ec2-instance-connect, ecr, ecr-public, ecs, efs, eks, elastic-inference, elasticache, elasticbeanstalk, elastictranscoder, elb, elbv2, emr, emr-containers, es, events, evidently, finspace, finspace-data, firehose, fis, fms, forecast, forecastquery, frauddetector, fsx, gamelift, glacier, globalaccelerator, glue, grafana, greengrass, greengrassv2, groundstation, guardduty, health, healthlake, honeycode, iam, identitystore, imagebuilder, importexport, inspector, inspector2, iot, iot-data, iot-jobs-data, iot1click-devices, iot1click-projects, iotanalytics, iotdeviceadvisor, iotevents, iotevents-data, iotfleethub, iotsecuretunneling, iotsitewise, iotthingsgraph, iottwinmaker, iotwireless, ivs, kafka, kafkaconnect, kendra, keyspaces, kinesis, kinesis-video-archived-media, kinesis-video-media, kinesis-video-signaling, kinesisanalytics, kinesisanalyticsv2, kinesisvideo, kms, lakeformation, lambda, lex-models, lex-runtime, lexv2-models, lexv2-runtime, license-manager, lightsail, location, logs, lookoutequipment, lookoutmetrics, lookoutvision, machinelearning, macie, macie2, managedblockchain, marketplace-catalog, marketplace-entitlement, marketplacecommerceanalytics, mediaconnect, mediaconvert, medialive, mediapackage, mediapackage-vod, mediastore, mediastore-data, mediatailor, memorydb, meteringmarketplace, mgh, mgn, migration-hub-refactor-spaces, migrationhub-config, migrationhubstrategy, mobile, mq, mturk, mwaa, neptune, network-firewall, networkmanager, nimble, opensearch, opsworks, opsworkscm, organizations, outposts, panorama, personalize, personalize-events, personalize-runtime, pi, pinpoint, pinpoint-email, pinpoint-sms-voice, polly, pricing, proton, qldb, qldb-session, quicksight, ram, rbin, rds, rds-data, redshift, redshift-data, rekognition, resiliencehub, resource-groups, resourcegroupstaggingapi, robomaker, route53, route53-recovery-cluster, route53-recovery-control-config, route53-recovery-readiness, route53domains, route53resolver, rum, s3, s3control, s3outposts, sagemaker, sagemaker-a2i-runtime, sagemaker-edge, sagemaker-featurestore-runtime, sagemaker-runtime, savingsplans, schemas, sdb, secretsmanager, securityhub, serverlessrepo, service-quotas, servicecatalog, servicecatalog-appregistry, servicediscovery, ses, sesv2, shield, signer, sms, sms-voice, snow-device-management, snowball, sns, sqs, ssm, ssm-contacts, ssm-incidents, sso, sso-admin, sso-oidc, stepfunctions, storagegateway, sts, support, swf, synthetics, textract, timestream-query, timestream-write, transcribe, transfer, translate, voice-id, waf, waf-regional, wafv2, wellarchitected, wisdom, workdocs, worklink, workmail, workmailmessageflow, workspaces, workspaces-web, xray I was trying to update some libraries like sagemaker, boto, boto3, but nothing seems to help.
I came across the same issue trying to run a machine learning tutorial by AWS in a notebook instance. What I had to do was update sagemaker within the notebook instance like so: import sys !{sys.executable} -m pip install sagemaker -U Hopefully this fixes your problem :)
I had the same issue, running pip install sagemaker -U solved it for me. Similar to the following issue: Sagemaker Studio UnkownServiceError for Session
"Invalid key: 0. Please first select a split. For example: `my_dataset_dictionary['train'][0]`. Available splits: ['train']"
I tried to use the dataset API loaded with my own data to train the hunggingface model. This is my code: train_data= datasets.load_dataset('csv', data_files="/gdrive/MyDrive/project/train.csv") test_data= datasets.load_dataset('csv', data_files="/gdrive/MyDrive/project/test.csv") train_data DatasetDict({ train: Dataset({ features: ['Post', 'Label'], num_rows: 174 }) }) But in the trainer, I get this error message. What happens to my dataset. I do not find any error. Could you help me? Thank you! trainer = Trainer( model=model, args=training_args, train_dataset=train_data, eval_dataset=test_data ) trainer.train() KeyError Traceback (most recent call last) <ipython-input-63-3435b262f1ae> in <module>() ----> 1 trainer.train() 5 frames /usr/local/lib/python3.7/dist-packages/datasets/dataset_dict.py in __getitem__(self, k) 44 suggested_split = available_suggested_splits[0] if available_suggested_splits else list(self)[0] 45 raise KeyError( ---> 46 f"Invalid key: {k}. Please first select a split. For example: " 47 f"`my_dataset_dictionary['{suggested_split}'][{k}]`. " 48 f"Available splits: {sorted(self)}" KeyError: "Invalid key: 0. Please first select a split. For example: `my_dataset_dictionary['train'][0]`. Available splits: ['train']"
NoSuchEntityException: An error occurred (NoSuchEntity) when calling the GetRole operation: The user with name <name> cannot be found
Call to get_execution_role() from notebook instance fails with the error message NoSuchEntityException: An error occurred (NoSuchEntity) when calling the GetRole operation: The user with name <name> cannot be found. Stack trace: NoSuchEntityExceptionTraceback (most recent call last) <ipython-input-1-1e2d3f162cfe> in <module>() 5 sagemaker_session = sagemaker.Session() 6 ----> 7 role = get_execution_role() /home/ec2-user/anaconda3/envs/tensorflow_p27/lib/python2.7/site-packages/sagemaker/session.pyc in get_execution_role(sagemaker_session) 871 if not sagemaker_session: 872 sagemaker_session = Session() --> 873 arn = sagemaker_session.get_caller_identity_arn() 874 875 if 'role' in arn: /home/ec2-user/anaconda3/envs/tensorflow_p27/lib/python2.7/site-packages/sagemaker/session.pyc in get_caller_identity_arn(self) 701 # Call IAM to get the role's path 702 role_name = role[role.rfind('/') + 1:] --> 703 role = self.boto_session.client('iam').get_role(RoleName=role_name)['Role']['Arn'] 704 705 return role /home/ec2-user/anaconda3/envs/tensorflow_p27/lib/python2.7/site-packages/botocore/client.pyc in _api_call(self, *args, **kwargs) 312 "%s() only accepts keyword arguments." % py_operation_name) 313 # The "self" in this scope is referring to the BaseClient. --> 314 return self._make_api_call(operation_name, kwargs) 315 316 _api_call.__name__ = str(py_operation_name) /home/ec2-user/anaconda3/envs/tensorflow_p27/lib/python2.7/site-packages/botocore/client.pyc in _make_api_call(self, operation_name, api_params) 610 error_code = parsed_response.get("Error", {}).get("Code") 611 error_class = self.exceptions.from_code(error_code) --> 612 raise error_class(parsed_response, operation_name) 613 else: 614 return parsed_response NoSuchEntityException: An error occurred (NoSuchEntity) when calling the GetRole operation: The user with name <name> cannot be found. However using boto client directly to get info about the role succeeds. This works fine: response = client.get_role( RoleName='role-name', )['Role']['Arn']
Turns out this is some weird bug that goes away if you stop and start the notebook instance.
I have shutdown and run again the notebook and it works. PD: I have to run again the code to make effect.
Django/pyodbc error: not enough arguments for format string
I have a Dictionary model defined in Django (1.6.5). One method (called get_topentities) returns the top names in my dictionary (entity names are defined by Entity model): def get_topentities(self,n): entities = self.entity_set.select_related().filter(in_dico=True,table_type=0).order_by("rank")[0:n] return entities When I call the function (say with n=2), it returns the top 2 elements but I cannot access the second one because of this "not enough arguments to format string" error: In [5]: d = Dictionary.objects.get(code='USA') In [6]: top2 = d.get_topentities(2) In [7]: top2 Out[7]: [<Entity: BARACK OBAMA>, <Entity: GOVERNMENT>] In [8]: top2[0] Out[8]: <Entity: BARACK OBAMA> In [9]: top2[1] . . /usr/local/lib/python2.7/dist-packages/django_pyodbc/compiler.pyc in as_sql(self, with_limits, with_col_aliases) 172 # Lop off ORDER... and the initial "SELECT" 173 inner_select = _remove_order_limit_offset(raw_sql) --> 174 outer_fields, inner_select = self._alias_columns(inner_select) 175 176 order = _get_order_limit_offset(raw_sql)[0] /usr/local/lib/python2.7/dist-packages/django_pyodbc/compiler.pyc in _alias_columns(self, sql) 339 340 # store the expanded paren string --> 341 parens[key] = buf% parens 342 #cannot use {} because IBM's DB2 uses {} as quotes 343 paren_buf[paren_depth] += '(%(' + key + ')s)' TypeError: not enough arguments for format string In [10]: My server backend is MSSQL and I'm using pyodbc as the database driver. If I try the same on a PC with engine sqlserver_ado, it works. Can someone help? Regards, Patrick
Complex query mongodb c
I've created my mongodb query like this > 86 bson query[1]; 87 bson_init(query); 88 bson_append_start_object(query, "service.virtual_mach ine"); 89 bson_append_oid(query,"_id",result); 90 bson_append_finish_object(query); 91 bson_finish(query); and i run it using this> 93 bson fields[1]; 94 bson_init(fields); 95 bson_append_oid(fields, "_id", result); 96 bson_finish(fields); 97 98 mongo_cursor* cursor = NULL; 99 cursor = mongo_find(conn, "db.services", query, field s, 9999,0,0); and if i print the "mongo_cursor_next(cursor)" method i got a "-1" (ERROR), i want to know, what's the error in my query. Thank you in advance.
This one? https://github.com/mongodb/mongo-c-driver#error-handling Most functions return MONGO_OK or BSON_OK on success and MONGO_ERROR or BSON_ERROR on failure. Specific error codes and error strings are then stored in the err and errstr fields of the mongo and bson objects. It is the client's responsibility to check for errors and handle them appropriately.