Advertisement
Guest User

Untitled

a guest
May 5th, 2012
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.01 KB | None | 0 0
  1. SQL Error: No such table
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5. android:layout_width="fill_parent"
  6. android:layout_height="fill_parent"
  7. android:orientation="vertical" >
  8.  
  9. <TextView
  10. android:layout_width="fill_parent"
  11. android:layout_height="wrap_content"
  12. android:text="Enter your name below:"
  13. />
  14.  
  15. <EditText
  16. android:id="@+id/nameText"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. />
  20.  
  21. <TextView
  22. android:layout_width="fill_parent"
  23. android:layout_height="wrap_content"
  24. android:text="Enter your langtitude below:"
  25. />
  26.  
  27. <EditText
  28. android:id="@+id/langText"
  29. android:layout_width="match_parent"
  30. android:layout_height="wrap_content"
  31. />
  32.  
  33. <TextView
  34. android:layout_width="fill_parent"
  35. android:layout_height="wrap_content"
  36. android:text="Enter your longtitude below:"
  37. />
  38.  
  39. <EditText
  40. android:id="@+id/longText"
  41. android:layout_width="match_parent"
  42. android:layout_height="wrap_content"
  43. />
  44.  
  45. <Button
  46. android:id="@+id/saveButton"
  47. android:layout_width="match_parent"
  48. android:layout_height="wrap_content"
  49. android:text="Save"
  50. />
  51.  
  52. <?xml version="1.0" encoding="utf-8"?>
  53. <ListView xmlns:android="http://schemas.android.com/apk/res/android"
  54. android:layout_width="fill_parent"
  55. android:layout_height="wrap_content"
  56. android:id="@+id/list">
  57. </ListView>
  58.  
  59. package com.mp.Testing;
  60.  
  61. import java.io.IOException;
  62.  
  63. import com.mp.Testing.DataAdapter;
  64. import com.mp.Testing.DataListActivity;
  65.  
  66. import android.content.ContentValues;
  67. import android.content.Context;
  68. import android.database.sqlite.SQLiteDatabase;
  69. import android.database.Cursor;
  70. import android.database.SQLException;
  71. import android.util.Log;
  72.  
  73. public class DataAdapter
  74. {
  75. // Name of the database
  76. private static final String DATABASE_NAME = "information";
  77.  
  78. // Names of the Tables in Database
  79. public static final String DATABASE_TABLE_1 = "pictures";
  80. public static final String DATABASE_TABLE_2 = "data";
  81.  
  82. // Version of the database
  83. private static final int DATABASE_VERSION = 1;
  84.  
  85. // Columns present in DATABASE_TABLE
  86. public static final String PICTURES_ROWID = "_id";
  87. public static final String PICTURES_FILE = "pictures_file";
  88. public static final String DATA_NAME = "_name";
  89. public static final String DATA_LANGTITUDE = "pictures_langtitude";
  90. public static final String DATA_LONGTITUDE = "pictures_longtitude";
  91.  
  92. // Help to create & manage the SQLiteDatabase
  93. private DataDBHelper DbHelper;
  94.  
  95. // CRUD on SQLiteDatabase
  96. private SQLiteDatabase database;
  97.  
  98. // SQL query string for creating DATABASE_TABLE_1
  99. static final String CREATE_DATABASE_TABLE_1 =
  100. "create table " + DATABASE_TABLE_1 + " (" + PICTURES_ROWID +
  101. " integer primary key autoincrement, " + PICTURES_FILE +
  102. " text not null);";
  103.  
  104. // SQL query string for creating DATABASE_TABLE_2
  105. static final String CREATE_DATABASE_TABLE_2 =
  106. "create table " + DATABASE_TABLE_2 + " (" + DATA_NAME +
  107. " integer primary key autoincrement, " + DATA_LANGTITUDE +
  108. " text not null, " + DATA_LONGTITUDE + " text not null);";
  109.  
  110. // Context object associated with the SQLite database object
  111. private final Context Ctx;
  112.  
  113. // Constructor
  114. public DataAdapter(Context ctx)
  115. {
  116. this.Ctx = ctx;
  117. }
  118.  
  119. // Open database connection
  120. public DataAdapter open() throws android.database.SQLException
  121. {
  122. DbHelper = new DataDBHelper(Ctx);
  123. database = DbHelper.getWritableDatabase();
  124. return this;
  125. }
  126.  
  127. // Close database connection
  128. public void close()
  129. {
  130. DbHelper.close();
  131. }
  132.  
  133. // Create the database_1 & define the values that is being insert
  134. public long createPictures(String file)
  135. {
  136. ContentValues initialValues = new ContentValues();
  137. initialValues.put(PICTURES_FILE, file);
  138.  
  139. return database.insert(DATABASE_TABLE_1, null, initialValues);
  140. }
  141.  
  142. // Create the database_2 & define the values that is being insert
  143. public long createData(String lan, String lon)
  144. {
  145. ContentValues initialValues = new ContentValues();
  146. initialValues.put(DATA_LANGTITUDE, lan);
  147. initialValues.put(DATA_LONGTITUDE, lon);
  148.  
  149. return database.insert(DATABASE_TABLE_2, null, initialValues);
  150. }
  151.  
  152. // Delete the ID in the database_1
  153. public boolean deletePictures(long picsId)
  154. {
  155. return database.delete(DATABASE_TABLE_1, PICTURES_ROWID + "=" + picsId, null) > 0;
  156. }
  157.  
  158. // Delete the ID in the database_2
  159. public boolean deleteData(long dataId)
  160. {
  161. return database.delete(DATABASE_TABLE_2, DATA_NAME + "=" + dataId, null) > 0;
  162. }
  163.  
  164. // Find all the data of database_1 from the system
  165. public Cursor fetchAllPictures()
  166. {
  167. return database.query(DATABASE_TABLE_1, new String[] {PICTURES_ROWID, PICTURES_FILE}, null, null, null, null, null);
  168. }
  169.  
  170. // Find all the data of database_2 from the system
  171. public Cursor fetchAllData()
  172. {
  173. return database.query(DATABASE_TABLE_2, new String[] {DATA_NAME, DATA_LANGTITUDE, DATA_LONGTITUDE}, null, null, null, null, null);
  174. }
  175.  
  176. // Fetch Pictures according to ID
  177. public Cursor fetchPictures(long picsId) throws SQLException
  178. {
  179. Cursor dCursor =
  180. database.query(true, DATABASE_TABLE_1, new String[] {PICTURES_ROWID,
  181. PICTURES_FILE}, PICTURES_ROWID + "=" +
  182. picsId, null, null, null, null, null);
  183.  
  184. // Go to the first record
  185. if (dCursor != null)
  186. {
  187. dCursor.moveToFirst();
  188. }
  189. return dCursor;
  190. }
  191.  
  192. // Fetch Data according to ID
  193. public Cursor fetchData(long dataId) throws SQLException
  194. {
  195. Cursor dCursor =
  196. database.query(true, DATABASE_TABLE_2, new String[] {DATA_NAME,
  197. DATA_LANGTITUDE, DATA_LONGTITUDE}, DATA_NAME + "=" +
  198. dataId, null, null, null, null, null);
  199.  
  200. // Go to the first record
  201. if (dCursor != null)
  202. {
  203. dCursor.moveToFirst();
  204. }
  205. return dCursor;
  206. }
  207.  
  208. // Update the database_1
  209. public boolean updatePictures(long picsId, String file)
  210. {
  211. ContentValues args = new ContentValues();
  212. args.put(PICTURES_FILE, file);
  213.  
  214. return database.update(DATABASE_TABLE_1, args, PICTURES_ROWID + "=" + picsId, null) > 0;
  215. }
  216.  
  217. // Update the database_2
  218. public boolean updateData(long dataId, String lan, String lon)
  219. {
  220. ContentValues args = new ContentValues();
  221. args.put(DATA_LANGTITUDE, lan);
  222. args.put(DATA_LONGTITUDE, lon);
  223.  
  224. return database.update(DATABASE_TABLE_2, args, DATA_NAME + "=" + dataId, null) > 0;
  225. }
  226. }
  227.  
  228. package com.mp.Testing;
  229.  
  230. import java.io.BufferedReader;
  231. import java.io.InputStream;
  232. import java.io.InputStreamReader;
  233.  
  234. import android.app.ListActivity;
  235. import android.content.ContentValues;
  236. import android.content.Context;
  237. import android.content.Intent;
  238. import android.database.Cursor;
  239. import android.os.Bundle;
  240. import android.util.Log;
  241. import android.view.MenuItem;
  242. import android.view.View;
  243. import android.widget.AdapterView.AdapterContextMenuInfo;
  244. import android.widget.ListView;
  245. import android.widget.SimpleCursorAdapter;
  246.  
  247. public class DataListActivity extends ListActivity
  248. {
  249. private static final int ACTIVITY_CREATE=0;
  250. private static final int ACTIVITY_EDIT=1;
  251.  
  252. // Define the variables
  253. private DataAdapter DbHelper;
  254.  
  255. /** Called when the activity is first created. */
  256. @Override
  257. public void onCreate(Bundle savedInstanceState)
  258. {
  259. super.onCreate(savedInstanceState);
  260. setContentView(R.layout.main);
  261.  
  262. DbHelper = new DataAdapter(this);
  263. DbHelper.open();
  264. fillData();
  265. registerForContextMenu(getListView());
  266. }
  267.  
  268. // Fill the data in the database
  269. private void fillPictures()
  270. {
  271. Cursor dbCursor = DbHelper.fetchAllData();
  272. startManagingCursor(dbCursor);
  273.  
  274. // Creating an array to specify the fields we want
  275. String[] dat = new String[]{DataAdapter.PICTURES_FILE};
  276.  
  277. // An array of the fields we want to bind in the view
  278. int[] dato = new int[]{R.id.nameText};
  279.  
  280. // Create a simple cursor adapter & display it
  281. SimpleCursorAdapter reminders = new SimpleCursorAdapter(this, R.layout.solution, dbCursor, dat, dato);
  282. setListAdapter(reminders);
  283. }
  284.  
  285. // Fill the data in the database
  286. private void fillData()
  287. {
  288. Cursor dbCursor = DbHelper.fetchAllData();
  289. startManagingCursor(dbCursor);
  290.  
  291. // Creating an array to specify the fields we want
  292. String[] lan = new String[]{DataAdapter.DATA_LANGTITUDE};
  293. String[] lon = new String[]{DataAdapter.DATA_LONGTITUDE};
  294.  
  295. // An array of the fields we want to bind in the view
  296. int[] lanto = new int[]{R.id.langText};
  297. int[] lonto = new int[]{R.id.longText};
  298.  
  299. // Create a simple cursor adapter & display it
  300. SimpleCursorAdapter landers = new SimpleCursorAdapter(this, R.layout.solution, dbCursor, lan, lanto);
  301. setListAdapter(landers);
  302.  
  303. // Create a simple cursor adapter & display it
  304. SimpleCursorAdapter londers = new SimpleCursorAdapter(this, R.layout.solution, dbCursor, lon, lonto);
  305. setListAdapter(londers);
  306. }
  307.  
  308. @Override
  309. protected void onListItemClick(ListView l, View v, int position, long id)
  310. {
  311. super.onListItemClick(l, v, position, id);
  312. Intent i = new Intent(this, DataEditActivity.class);
  313. i.putExtra(DataAdapter.PICTURES_ROWID, id);
  314. i.putExtra(DataAdapter.DATA_NAME, id);
  315. startActivityForResult(i, ACTIVITY_EDIT);
  316. }
  317.  
  318. @Override
  319. protected void onActivityResult(int requestCode, int resultCode, Intent intent)
  320. {
  321. super.onActivityResult(requestCode, resultCode, intent);
  322. fillData();
  323. }
  324.  
  325. @Override
  326. public boolean onContextItemSelected(MenuItem item)
  327. {
  328. switch(item.getItemId())
  329.  
  330. {
  331. case R.id.list:
  332. AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
  333. DbHelper.deleteData(info.id);
  334. fillData();
  335. return true;
  336. }
  337. return super.onContextItemSelected(item);
  338. }
  339. }
  340.  
  341. package com.mp.Testing;
  342.  
  343. import android.app.Activity;
  344. import android.content.Intent;
  345. import android.database.Cursor;
  346. import android.os.Bundle;
  347. import android.util.Log;
  348. import android.view.View;
  349. import android.view.View.OnClickListener;
  350. import android.widget.Button;
  351. import android.widget.EditText;
  352. import android.widget.ListView;
  353. import android.widget.SimpleCursorAdapter;
  354. import android.widget.Toast;
  355.  
  356. public class DataEditActivity extends Activity implements OnClickListener
  357. {
  358. private DataAdapter DbHelper;
  359. private Long mPicsId;
  360. private Long mDataId;
  361. private EditText ET;
  362. private EditText LAT;
  363. private EditText LOT;
  364. private Button SB;
  365.  
  366. @Override
  367. protected void onCreate(Bundle savedInstanceState)
  368. {
  369. super.onCreate(savedInstanceState);
  370. setContentView(R.layout.main);
  371.  
  372. DbHelper = new DataAdapter(this);
  373.  
  374. ET = (EditText) findViewById(R.id.nameText);
  375. LAT = (EditText) findViewById(R.id.langText);
  376. LOT = (EditText) findViewById(R.id.longText);
  377. SB = (Button) findViewById(R.id.saveButton);
  378.  
  379. mPicsId = savedInstanceState != null
  380. ? savedInstanceState.getLong(DataAdapter.PICTURES_ROWID): null;
  381. mDataId = savedInstanceState != null
  382. ? savedInstanceState.getLong(DataAdapter.DATA_NAME): null;
  383. registerButtonListenersAndSetDefaultText();
  384. }
  385.  
  386. private void registerButtonListenersAndSetDefaultText()
  387. {
  388. // TODO Auto-generated method stub
  389. SB.setOnClickListener(new View.OnClickListener()
  390. {
  391. public void onClick(View view)
  392. {
  393. saveState();
  394. setResult(RESULT_OK);
  395. Toast.makeText(DataEditActivity.this,
  396. getString(R.string.message),
  397. Toast.LENGTH_SHORT).show();
  398. finish();
  399. }
  400. });
  401. }
  402.  
  403. // Intent to start the activity
  404. private void setRowIdFromIntent()
  405. {
  406. if (mPicsId == null)
  407. {
  408. Bundle extras = getIntent().getExtras();
  409. mPicsId = extras != null
  410. ? extras.getLong(DataAdapter.PICTURES_ROWID): null;
  411. }
  412. if (mDataId == null)
  413. {
  414. Bundle extras = getIntent().getExtras();
  415. mDataId = extras != null
  416. ? extras.getLong(DataAdapter.DATA_NAME): null;
  417. }
  418. }
  419.  
  420. // Database is close when it is pause
  421. @Override
  422. protected void onPause()
  423. {
  424. super.onPause();
  425. DbHelper.close();
  426. }
  427.  
  428. // Resume the database
  429. @Override
  430. protected void onResume()
  431. {
  432. super.onResume();
  433. DbHelper.open();
  434. setRowIdFromIntent();
  435. populateFields();
  436. }
  437.  
  438. // Populate the form
  439. private void populateFields()
  440. {
  441. if (mPicsId != null)
  442. {
  443. Cursor pics = DbHelper.fetchData(mPicsId);
  444. startManagingCursor(pics);
  445.  
  446. ET.setText(pics.getString(
  447. pics.getColumnIndexOrThrow(DataAdapter.PICTURES_FILE)));
  448. }
  449. if (mDataId != null)
  450. {
  451. Cursor data = DbHelper.fetchData(mDataId);
  452. startManagingCursor(data);
  453.  
  454. LAT.setText(data.getString(
  455. data.getColumnIndexOrThrow(DataAdapter.DATA_LANGTITUDE)));
  456. LOT.setText(data.getString(
  457. data.getColumnIndexOrThrow(DataAdapter.DATA_LONGTITUDE)));
  458. }
  459. }
  460.  
  461. @Override
  462. protected void onSaveInstanceState(Bundle outState)
  463. {
  464. super.onSaveInstanceState(outState);
  465. outState.putLong(DataAdapter.PICTURES_ROWID, mPicsId);
  466. outState.putLong(DataAdapter.DATA_NAME, mDataId);
  467. }
  468.  
  469. private void saveState()
  470. {
  471. String file = ET.getText().toString();
  472. String lan = LAT.getText().toString();
  473. String lon = LOT.getText().toString();
  474.  
  475. if (mPicsId == null && mDataId == null)
  476. {
  477. long id = DbHelper.createPictures(file);
  478. long ild = DbHelper.createData(lan, lon);
  479.  
  480. if (id > 0 && ild > 0)
  481. {
  482. mPicsId = id;
  483. mDataId = ild;
  484. }
  485. }
  486. else
  487. {
  488. DbHelper.updatePictures(mPicsId, file);
  489. DbHelper.updateData(mDataId, lan, lon);
  490. }
  491. }
  492.  
  493. @Override
  494. public void onClick(View v)
  495. {
  496. Intent i = new Intent(DataEditActivity.this, DataEditActivity.class);
  497. startActivity(i);
  498. }
  499. }
  500.  
  501. package com.mp.Testing;
  502.  
  503. import java.io.BufferedReader;
  504. import java.io.InputStream;
  505. import java.io.InputStreamReader;
  506.  
  507. import android.content.ContentValues;
  508. import android.content.Context;
  509. import android.database.sqlite.SQLiteDatabase;
  510. import android.database.sqlite.SQLiteOpenHelper;
  511. import android.util.Log;
  512.  
  513. public class DataDBHelper extends SQLiteOpenHelper
  514. {
  515. // Name & the version of Database.
  516. public static final String DATABASE_NAME = "information";
  517. public static final int DATABASE_VERSION = 1;
  518.  
  519. // Names of the Tables in Database
  520. public static final String DATABASE_TABLE_1 = "pictures";
  521. public static final String DATABASE_TABLE_2 = "data";
  522.  
  523. // Columns present in DATABASE_TABLE
  524. public static final String PICTURES_ROWID = "_id";
  525. public static final String PICTURES_FILE = "pictures_file";
  526. public static final String DATA_NAME = "_name";
  527. public static final String DATA_LANGTITUDE = "pictures_langtitude";
  528. public static final String DATA_LONGTITUDE = "pictures_longtitude";
  529.  
  530. // SQL query string for creating DATABASE_TABLE_1
  531. static final String CREATE_DATABASE_TABLE_1 =
  532. "create table " + DATABASE_TABLE_1 + " (" + PICTURES_ROWID +
  533. " integer primary key autoincrement, " + PICTURES_FILE +
  534. " text not null);";
  535.  
  536. // SQL query string for creating DATABASE_TABLE_2
  537. static final String CREATE_DATABASE_TABLE_2 =
  538. "create table " + DATABASE_TABLE_2 + " (" + DATA_NAME +
  539. " integer primary key autoincrement, " + DATA_LANGTITUDE +
  540. " text not null, " + DATA_LONGTITUDE + " text not null);";
  541.  
  542. // To execute the SQL command
  543. @Override
  544. public void onCreate(SQLiteDatabase database)
  545. {
  546. database.execSQL(CREATE_DATABASE_TABLE_1);
  547. database.execSQL(CREATE_DATABASE_TABLE_2);
  548. Log.d("SaveData", "Created DB");
  549. }
  550.  
  551. public static final String TAG_1 = "PICTURES_TABLE";
  552. public static final String TAG_2 = "DATA_TABLE";
  553.  
  554. private Context context;
  555.  
  556. // Constructor
  557. public DataDBHelper(Context context)
  558. {
  559. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  560. this.context = context;
  561. }
  562.  
  563. // Upgrading the database version
  564. @Override
  565. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
  566. {
  567. // TODO Auto-generated method stub
  568. }
  569.  
  570. // Inserting pictures into database
  571. private void insertDataIntoPictures(SQLiteDatabase db)
  572. {
  573. try
  574. {
  575. InputStream is = context.getResources().openRawResource(R.raw.picture);
  576. BufferedReader br = new BufferedReader(new InputStreamReader(is));
  577. String strLine = null;
  578.  
  579. while ((strLine = (br.readLine()).trim()) != null)
  580. {
  581. String[] temp = null;
  582.  
  583. ContentValues initialValues = new ContentValues();
  584.  
  585. initialValues.put(PICTURES_FILE, temp[0].trim());
  586.  
  587. db.insert(DATABASE_TABLE_1, null, initialValues);
  588. }
  589. is.close();
  590. }
  591. catch (Exception e)
  592. {
  593. Log.d(TAG_1, "Error while inserting common names into table");
  594. }
  595. }
  596.  
  597. // Inserting data into database
  598. private void insertDataIntoData(SQLiteDatabase db)
  599. {
  600. try
  601. {
  602. InputStream is = context.getResources().openRawResource(R.raw.data);
  603. BufferedReader br = new BufferedReader(new InputStreamReader(is));
  604. String strLine = null;
  605.  
  606. while ((strLine = (br.readLine()).trim()) != null)
  607. {
  608. String[] temp = null;
  609.  
  610. ContentValues initialValues = new ContentValues();
  611.  
  612. initialValues.put(DATA_LANGTITUDE, temp[0]);
  613. initialValues.put(DATA_LONGTITUDE, temp[1]);
  614.  
  615. db.insert(DATABASE_TABLE_2, null, initialValues);
  616. }
  617. is.close();
  618. }
  619. catch (Exception e)
  620. {
  621. Log.d(TAG_2, "Error while inserting common names into table");
  622. }
  623. }
  624. }
  625.  
  626. @Override
  627. public void onCreate(SQLiteDatabase database)
  628. {
  629. database.execSQL(CREATE_DATABASE_TABLE_1);
  630. Log.d("SaveData", "Created DB");
  631. }
  632.  
  633. @Override
  634. public void onCreate(SQLiteDatabase database)
  635. {
  636. database.execSQL(CREATE_DATABASE_TABLE_1);
  637. database.execSQL(CREATE_DATABASE_TABLE_2);
  638. Log.d("SaveData", "Created DB");
  639. }
  640.  
  641. @Override
  642. public void onCreate(SQLiteDatabase database) { database.execSQL(CREATE_DATABASE_TABLE_1);
  643. Log.d("SaveData", "Created DB"); }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement