Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

translate a code

Discussion in 'Editor & General Support' started by sawi, Mar 11, 2015.

  1. sawi

    sawi

    Joined:
    Feb 24, 2015
    Posts:
    11
    hello
    i am nem in unitu and in programming in general can anyone help me to translate this code into a c#

    public var DatabaseName : String = "TestDB.sqlite";

    // This is the name of the table we want to use
    public var TableName : String = "TestTable";
    var db : dbAccess;

    function Start()
    {
    // Give ourselves a dbAccess object to work with, and open it
    db = new dbAccess();
    db.OpenDB(DatabaseName);
    // Let's make sure we've got a table to work with as well!
    var tableName = TableName;
    var columnNames = new Array("firstName","lastName");
    var columnValues = new Array("text","text");

    try
    {
    db.CreateTable(tableName,columnNames,columnValues) ;
    }

    catch(e)// Do nothing - our table was already created
    {
    //- we don't care about the error, we just don't want to see it
    }
    }

    // These variables just hold info to display in our GUI
    var firstName : String = "First Name";
    var lastName : String = "Last Name";
    var DatabaseEntryStringWidth = 100;
    var scrollPosition : Vector2;
    var databaseData : ArrayList = new ArrayList();

    // This GUI provides us with a way to enter data into our database
    // as well as a way to view it

    function OnGUI()
    {
    GUI.Box(Rect (25,25,Screen.width - 50, Screen.height - 50),"");
    GUILayout.BeginArea(Rect(50, 50, Screen.width - 100, Screen.height - 100));

    // This first block allows us to enter new entries into our table
    GUILayout.BeginHorizontal();
    firstName = GUILayout.TextField(firstName, GUILayout.Width (DatabaseEntryStringWidth));
    lastName = GUILayout.TextField(lastName, GUILayout.Width (DatabaseEntryStringWidth));
    GUILayout.EndHorizontal();

    if (GUILayout.Button("Add to database"))
    {
    // Insert the data
    InsertRow(firstName,lastName);
    // And update the readout of the database
    databaseData = ReadFullTable();
    }
    // This second block gives us a button that will display/refresh the contents of our database
    GUILayout.BeginHorizontal();
    if (GUILayout.Button ("Read Database"))
    databaseData = ReadFullTable();
    if (GUILayout.Button("Clear"))
    databaseData.Clear();
    GUILayout.EndHorizontal();

    GUILayout.Label("Database Contents");
    scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Height(100));
    for (var line : ArrayList in databaseData)
    {
    GUILayout.BeginHorizontal();
    for (var s in line)
    {
    GUILayout.Label(s.ToString(), GUILayout.Width(DatabaseEntryStringWidth));
    }
    GUILayout.EndHorizontal();
    }

    GUILayout.EndScrollView();

    if (GUILayout.Button("Delete All Data"))
    {
    DeleteTableContents();
    databaseData = ReadFullTable();
    }

    GUILayout.EndArea();
    }

    // Wrapper function for inserting our specific entries into our specific database and table for this file
    function InsertRow(firstName, lastName)
    {
    var values = new Array(("'"+firstName+"'"),("'"+lastName+"'"));
    db.InsertInto(TableName, values);
    }

    // Wrapper function, so we only mess with our table.
    function ReadFullTable()
    {
    return db.ReadFullTable(TableName);
    }

    // Another wrapper function...
    function DeleteTableContents()
    {
    db.DeleteTableContents(TableName);
    }
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,000
  3. sawi

    sawi

    Joined:
    Feb 24, 2015
    Posts:
    11
    Code (CSharp):
    1. public var DatabaseName : String = "TestDB.sqlite";
    2.  
    3. // This is the name of the table we want to use
    4. public var TableName : String = "TestTable";
    5. var db : dbAccess;
    6.  
    7. function Start()
    8. {
    9. // Give ourselves a dbAccess object to work with, and open it
    10. db = new dbAccess();
    11. db.OpenDB(DatabaseName);
    12. // Let's make sure we've got a table to work with as well!
    13. var tableName = TableName;
    14. var columnNames = new Array("firstName","lastName");
    15. var columnValues = new Array("text","text");
    16.  
    17. try
    18. {
    19. db.CreateTable(tableName,columnNames,columnValues) ;
    20. }
    21.  
    22. catch(e)// Do nothing - our table was already created
    23. {
    24. //- we don't care about the error, we just don't want to see it
    25. }
    26. }
    27.  
    28. // These variables just hold info to display in our GUI
    29. var firstName : String = "First Name";
    30. var lastName : String = "Last Name";
    31. var DatabaseEntryStringWidth = 100;
    32. var scrollPosition : Vector2;
    33. var databaseData : ArrayList = new ArrayList();
    34.  
    35. // This GUI provides us with a way to enter data into our database
    36. // as well as a way to view it
    37.  
    38. function OnGUI()
    39. {
    40. GUI.Box(Rect (25,25,Screen.width - 50, Screen.height - 50),"");
    41. GUILayout.BeginArea(Rect(50, 50, Screen.width - 100, Screen.height - 100));
    42.  
    43. // This first block allows us to enter new entries into our table
    44. GUILayout.BeginHorizontal();
    45. firstName = GUILayout.TextField(firstName, GUILayout.Width (DatabaseEntryStringWidth));
    46. lastName = GUILayout.TextField(lastName, GUILayout.Width (DatabaseEntryStringWidth));
    47. GUILayout.EndHorizontal();
    48.  
    49. if (GUILayout.Button("Add to database"))
    50. {
    51. // Insert the data
    52. InsertRow(firstName,lastName);
    53. // And update the readout of the database
    54. databaseData = ReadFullTable();
    55. }
    56. // This second block gives us a button that will display/refresh the contents of our database
    57. GUILayout.BeginHorizontal();
    58. if (GUILayout.Button ("Read Database"))
    59. databaseData = ReadFullTable();
    60. if (GUILayout.Button("Clear"))
    61. databaseData.Clear();
    62. GUILayout.EndHorizontal();
    63.  
    64. GUILayout.Label("Database Contents");
    65. scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Height(100));
    66. for (var line : ArrayList in databaseData)
    67. {
    68. GUILayout.BeginHorizontal();
    69. for (var s in line)
    70. {
    71. GUILayout.Label(s.ToString(), GUILayout.Width(DatabaseEntryStringWidth));
    72. }
    73. GUILayout.EndHorizontal();
    74. }
    75.  
    76. GUILayout.EndScrollView();
    77.  
    78. if (GUILayout.Button("Delete All Data"))
    79. {
    80. DeleteTableContents();
    81. databaseData = ReadFullTable();
    82. }
    83.  
    84. GUILayout.EndArea();
    85. }
    86.  
    87. // Wrapper function for inserting our specific entries into our specific database and table for this file
    88. function InsertRow(firstName, lastName)
    89. {
    90. var values = new Array(("'"+firstName+"'"),("'"+lastName+"'"));
    91. db.InsertInto(TableName, values);
    92. }
    93.  
    94. // Wrapper function, so we only mess with our table.
    95. function ReadFullTable()
    96. {
    97. return db.ReadFullTable(TableName);
    98. }
    99.  
    100. // Another wrapper function...
    101. function DeleteTableContents()
    102. {
    103. db.DeleteTableContents(TableName);
    104. }