Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.72 KB | None | 0 0
  1. String fname , lname, email;
  2. SQLiteDatabase db;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7. db=openOrCreateDatabase("MYDB1",MODE_PRIVATE , null);
  8. db.execSQL("CREATE TABLE IF NOT TEXISTS Student(fname VARCHAR , lname VARCHAR , email VARCHAR);");
  9. }
  10. private void Adddata(View view)
  11. {
  12.  
  13. EditText edittext1=(EditText) findViewById(R.id.firstname);
  14. EditText edittext2=(EditText) findViewById(R.id.lastname);
  15. EditText edittext3=(EditText) findViewById(R.id.email);
  16.  
  17. fname=edittext1.getText().toString();
  18. lname=edittext2.getText().toString();
  19. email=edittext3.getText().toString();
  20.  
  21. db.execSQL("INSERT INTO Student Values('"+fname+"', '"+lname+"','"+email+"');");
  22. }
  23.  
  24. private void Showdata(View view)
  25. {
  26. Cursor c=db.rawQuery("SELECT * from Student", null);
  27.  
  28. int count =c.getCount();
  29. c.moveToFirst();
  30. TableLayout tableLayout = new TableLayout(getApplicationContext());
  31. tableLayout.setVerticalScrollBarEnabled(true);
  32. TableRow tableRow;
  33. TextView textView,textView1,textView2,textView3,textView4,textView5;
  34.  
  35. tableRow = new TableRow(getApplicationContext());
  36. textView = new TextView(getApplicationContext());
  37.  
  38.  
  39. textView.setText("FirstName");
  40. textView.setTextColor(Color.RED);
  41. textView.setTypeface(null,Typeface.BOLD);
  42. textView.setPadding(20, 20, 20, 20);
  43. tableRow.addView(textView);
  44.  
  45. textView4=new TextView(getApplicationContext());
  46. textView4.setText("LastName");
  47. textView4.setTextColor(Color.RED);
  48. textView4.setTypeface(null,Typeface.BOLD);
  49. textView4.setPadding(20, 20, 20, 20);
  50. tableRow.addView(textView4);
  51.  
  52. textView5=new TextView(getApplicationContext());
  53. textView5.setText("Email");
  54. textView5.setTextColor(Color.RED);
  55. textView5.setTypeface(null,Typeface.BOLD);
  56. textView5.setPadding(20, 20, 20, 20);
  57. tableRow.addView(textView5);
  58.  
  59. tableLayout.addView(tableRow);
  60.  
  61. for (Integer j = 0; j< count; j++)
  62.  
  63. {
  64. tableRow = new TableRow(getApplicationContext());
  65. textView1 = new TextView(getApplicationContext());
  66. textView1.setText(c.getString(c.getColumnIndex("fname")));
  67.  
  68. textView2 = new TextView(getApplicationContext());
  69. textView2.setText(c.getString(c.getColumnIndex("lname")));
  70.  
  71. textView3 = new TextView(getApplicationContext());
  72. textView3.setText(c.getString(c.getColumnIndex("email")));
  73.  
  74. textView1.setPadding(20, 20, 20,20);
  75. textView2.setPadding(20, 20, 20,20);
  76. textView3.setPadding(20, 20, 20,20);
  77.  
  78. tableRow.addView(textView1);
  79.  
  80. tableRow.addView(textView2);
  81.  
  82. tableRow.addView(textView3);
  83.  
  84. tableLayout.addView(tableRow);
  85. c.moveToNext();
  86. }
  87.  
  88. setContentView(tableLayout);
  89. db.close();
  90. }
  91.  
  92. public void close(View view)
  93. {
  94. System.exit(0);
  95. }
  96.  
  97. 04-15 05:33:35.650: D/dalvikvm(929): Not late-enabling CheckJNI (already on)
  98. 04-15 05:33:37.460: E/SQLiteLog(929): (1) near "TEXISTS": syntax error
  99. 04-15 05:33:37.460: D/AndroidRuntime(929): Shutting down VM
  100. 04-15 05:33:37.460: W/dalvikvm(929): threadid=1: thread exiting with uncaught exception (group=0xb4aebba8)
  101. 04-15 05:33:37.470: E/AndroidRuntime(929): FATAL EXCEPTION: main
  102. 04-15 05:33:37.470: E/AndroidRuntime(929): Process: com.example.studentdatabase, PID: 929
  103. 04-15 05:33:37.470: E/AndroidRuntime(929): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.studentdatabase/com.example.studentdatabase.MainActivity}: android.database.sqlite.SQLiteException: near "TEXISTS": syntax error (code 1): , while compiling: CREATE TABLE IF NOT TEXISTS Student(fname VARCHAR , lname VARCHAR , email VARCHAR);
  104. 04-15 05:33:37.470: E/AndroidRuntime(929): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
  105. 04-15 05:33:37.470: E/AndroidRuntime(929): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
  106. 04-15 05:33:37.470: E/AndroidRuntime(929): at android.app.ActivityThread.access$800(ActivityThread.java:135)
  107. 04-15 05:33:37.470: E/AndroidRuntime(929): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
  108. 04-15 05:33:37.470: E/AndroidRuntime(929): at android.os.Handler.dispatchMessage(Handler.java:102)
  109. 04-15 05:33:37.470: E/AndroidRuntime(929): at android.os.Looper.loop(Looper.java:136)
  110. 04-15 05:33:37.470: E/AndroidRuntime(929): at android.app.ActivityThread.main(ActivityThread.java:5017)
  111. 04-15 05:33:37.470: E/AndroidRuntime(929): at java.lang.reflect.Method.invokeNative(Native Method)
  112. 04-15 05:33:37.470: E/AndroidRuntime(929): at java.lang.reflect.Method.invoke(Method.java:515)
  113. 04-15 05:33:37.470: E/AndroidRuntime(929): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
  114. 04-15 05:33:37.470: E/AndroidRuntime(929): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
  115. 04-15 05:33:37.470: E/AndroidRuntime(929): at dalvik.system.NativeStart.main(Native Method)
  116. 04-15 05:33:37.470: E/AndroidRuntime(929): Caused by: android.database.sqlite.SQLiteException: near "TEXISTS": syntax error (code 1): , while compiling: CREATE TABLE IF NOT TEXISTS Student(fname VARCHAR , lname VARCHAR , email VARCHAR);
  117. 04-15 05:33:37.470: E/AndroidRuntime(929): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
  118. 04-15 05:33:37.470: E/AndroidRuntime(929): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
  119. 04-15 05:33:37.470: E/AndroidRuntime(929): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
  120. 04-15 05:33:37.470: E/AndroidRuntime(929): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
  121. 04-15 05:33:37.470: E/AndroidRuntime(929): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
  122. 04-15 05:33:37.470: E/AndroidRuntime(929): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
  123. 04-15 05:33:37.470: E/AndroidRuntime(929): at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1672)
  124. 04-15 05:33:37.470: E/AndroidRuntime(929): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1603)
  125. 04-15 05:33:37.470: E/AndroidRuntime(929): at com.example.studentdatabase.MainActivity.onCreate(MainActivity.java:26)
  126. 04-15 05:33:37.470: E/AndroidRuntime(929): at android.app.Activity.performCreate(Activity.java:5231)
  127. 04-15 05:33:37.470: E/AndroidRuntime(929): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
  128. 04-15 05:33:37.470: E/AndroidRuntime(929): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
  129. 04-15 05:33:37.470: E/AndroidRuntime(929): ... 11 more
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement