Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed May 13, 2014 at 12:27 PM by Cerbion_ for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Cerbion_ · Jul 13, 2013 at 06:23 PM · c#variablesdatabasesql

How to pass SQL vars?

Hey, I am not that experienced with SQL in Unity (or rather with c#)

However I am trying to use my webserver (MySQL server) for storing the user data, here's my function a good friend wrote for me to do SQL queries:

 // MySQL Query
     public static void doQuery(string sqlQuery)
     {
         IDbCommand dbCommand = dbConnection.CreateCommand();
         dbCommand.CommandText = sqlQuery;
         IDataReader reader = dbCommand.ExecuteReader();
         reader.Close();
         reader = null;
         dbCommand.Dispose();
         dbCommand = null;
     }

which works very well for storing data, but In order to store data, there need to be users in the database.

As for the Login/Register, I am trying to use this codesegment:

 DB.doQuery("SELECT ID, PlayerPassword FROM users WHERE PlayerName = '"+username+"';");

And that should actually work, but my main concern is:

How are the variables (ID and PlayerPassword) passed to C#/Unity? Or do I have to alter the doQuery function, as it actually returns no values whatsoever (which is unsurprising since the function is void.)

Thanks in advance :3

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image supercouge · Jul 13, 2013 at 09:22 PM 1
Share

Just like you said, you should alter the doQuery function in order to get access to the reader.

 // just a little example (its faaar from being good but it shouldgive an idea of how to proceed):
 
 //in doQuery, after reader has been initialized.
 string[] str = new string[2];
 reader.Read() // only one element
 str[0] = reader.GetString(reader.GetOrdinal("ID")));
 str[1] = reader.GetString(reader.GetOrdinal("PlayerPassword")));
 ..
 ..
 //end of doQuery
 return str;

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by Cerbion_ · Jul 14, 2013 at 02:51 AM

Thanks to 'supercouge' the solution is:

 (..)
 reader.Read ();
 int returnValue;
 try
 {
     result[0] = reader.GetString(reader.GetOrdinal("PlayerID"));
     returnValue = int.Parse(result[0]);
 }
 catch (Exception ex)
 {
     returnValue = -1;
     Debug.Log ("Error: "+ex);
 }
 (..)

And then DB.doQuery(..) returns either -1 if there is no player with that name, or the ID as an integer.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image supercouge · Jul 14, 2013 at 08:33 AM 0
Share

You should also test, in case, that there is one and only one element which is returned by your SQL request.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

MYSQL How do get all of my SELECT information? 2 Answers

Is it possible to create GameObjects dinamically from a server? 1 Answer

using SQL db instead of playerprefs (playerlocation) 0 Answers

Microsoft SQL Server error. 0 Answers

Updating variables from dictionary database in edit mode C# 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges