Facebook
From Kamil, 9 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 541
  1. package com.example.test2;
  2.  
  3.  
  4. import android.app.Activity;
  5. import android.database.Cursor;
  6. import android.database.SQLException;
  7. import android.database.sqlite.SQLiteDatabase;
  8. import android.database.sqlite.SQLiteOpenHelper;
  9. //import android.database.sqlite.SQLiteQueryBuilder;
  10. import android.os.Bundle;
  11. import android.util.Log;
  12. import android.view.Menu;
  13. import android.view.MenuItem;
  14. import android.view.View;
  15. import android.view.View.OnClickListener;
  16. import android.widget.Button;
  17. import android.widget.EditText;
  18. import android.widget.TextView;
  19.  
  20. public class BazaActivity extends Activity {
  21.  
  22.         Button pokaz, pokaz2,dodaj;
  23.         TextView imie, textGora,wyszukany;
  24.         EditText dodaj_imie, dodaj_nazwisko,szukaj_im;
  25.         SQLiteDatabase dbTest;// = null;
  26.         SQLiteOpenHelper opener;
  27.        
  28.         @Override
  29.         protected void onCreate(Bundle savedInstanceState) {
  30.                
  31.                 super.onCreate(savedInstanceState);
  32.                 setContentView(R.layout.activity_baza);
  33.                 pokaz = (Button) findViewById(R.id.button1);
  34.                 dodaj = (Button) findViewById(R.id.button2);
  35.                 pokaz2 = (Button) findViewById(R.id.button3);
  36.                 imie = (TextView) findViewById(R.id.textView2);
  37.                 textGora = (TextView) findViewById(R.id.textView1);
  38.                 wyszukany = (TextView) findViewById(R.id.textView3);
  39.                 dodaj_imie=(EditText)findViewById(R.id.editText1);
  40.                 dodaj_nazwisko = (EditText) findViewById(R.id.editText2);
  41.                 szukaj_im = (EditText) findViewById(R.id.editText3);
  42.                 dbTest = this.openOrCreateDatabase("newdb.db", MODE_PRIVATE, null);
  43.                 //opener.onOpen(dbTest);
  44.                 //opener.
  45.                 /*dbTest.execSQL("DROP TABLE IF EXISTS USER;");
  46.                 dbTest.execSQL("CREATE TABLE USER (" + "ID INTEGER PRIMARY KEY AUTOINCREMENT," +
  47.                                         "NAZWISKO VARCHAR (20)," +
  48.                                         "IMIE VARCHAR (20)"
  49.                                         + "); ");
  50.                 dbTest.execSQL("INSERT INTO USER (IMIE, NAZWISKO) VALUES ('KinGA', 'SIELCZAK');");
  51.                 dbTest.execSQL("INSERT INTO USER "+"(IMIE, NAZWISKO) VALUES (" + "'KAMIL', 'ZAPOTOCZNY');");
  52.                 Log.d("Baza sqlite", "dodanie");
  53.                 */pokaz.setOnClickListener(
  54.                                 new OnClickListener(){
  55.  
  56.                                         @Override
  57.                                         public void onClick(View v) {
  58.                                                 // TODO Auto-generated method stub
  59.                                                 //imie.setText("Test podmiana");
  60.                                                 wczytajUsera(); // metoda nie działa
  61.                                         }
  62.                                        
  63.                                 }
  64.                                 );
  65.                
  66.                 dodaj.setOnClickListener(
  67.                                 new OnClickListener() {
  68.                        
  69.                         @Override
  70.                         public void onClick(View v) {
  71.                                 // TODO Auto-generated method stub
  72.                         dodajUsera(dodaj_imie.getText().toString(), dodaj_nazwisko.getText().toString());      
  73.                         }
  74.                 });
  75.                
  76.                 pokaz2.setOnClickListener(
  77.                                 new OnClickListener(){
  78.  
  79.                                         @Override
  80.                                         public void onClick(View v) {
  81.                                                 // TODO Auto-generated method stub
  82.                                                 //imie.setText("Test podmiana");
  83.                                                 wczytajUsera(szukaj_im.getText().toString()); // metoda nie działa
  84.                                         }
  85.                                        
  86.                                 }
  87.                                 );
  88.        
  89.         }
  90.  
  91.        
  92.         public void ustawParam(){
  93.                 ;
  94.         }
  95.        
  96.         public String wczytajUsera(){
  97.                 String s ="";
  98.                 try{
  99.                 //Cursor c=dbTest.rawQuery("SELECT IMIE FROM USER WHERE NAZWISKO = "+ "\"SIELCZAK\"", null);
  100.                 //Cursor c=dbTest.rawQuery("SELECT * FROM USER where NAZWISKO = "+"\"ZAPOTOCZNY\"", null);
  101.                 Cursor c=dbTest.rawQuery("SELECT * FROM USER where NAZWISKO = 'ZAPOTOCZNY'", null);
  102.                 if(c.moveToFirst()){
  103.                 s=c.getString(c.getColumnIndex("IMIE"));
  104.                 //s=c.toString();
  105.                 }
  106.                 }catch(SQLException e){
  107.                         s=e.toString();
  108.                        
  109.                 }
  110.                 //s= c.getString(1);
  111.                 //imie.setText(s);
  112.                 //String st=dodajUsera("Ciapa", "Rapa");
  113.                 imie.setText(s);
  114.                
  115.                 //System.out.println("Wynik zapytania po kliknięciu ");
  116.                 return s;
  117.         }
  118.        
  119.        
  120.         public String wczytajUsera(String szukaj_imie){
  121.                 String s ="";
  122.                 try{
  123.                 //Cursor c=dbTest.rawQuery("SELECT IMIE FROM USER WHERE NAZWISKO = "+ "\"SIELCZAK\"", null);
  124.                 //Cursor c=dbTest.rawQuery("SELECT * FROM USER where NAZWISKO = "+"\"ZAPOTOCZNY\"", null);
  125.                
  126.                 String ss="SELECT * FROM USER where IMIE LIKE '%";     
  127.                 StringBuffer sbuf = new StringBuffer(ss);
  128.                 //sbuf.insert(0, "");
  129.                 sbuf.append(szukaj_imie);
  130.                 sbuf.append("%'");
  131.                 ss=sbuf.toString();
  132.        
  133.                 Cursor c=dbTest.rawQuery(ss, null);
  134.                 //Cursor c=dbTest.rawQuery("SELECT * FROM USER where NAZWISKO = 'ZAPOTOCZNY'", null);
  135.                 if(c.moveToFirst()){
  136.                 s=c.getString(c.getColumnIndex("IMIE"));
  137.                 //s=c.toString();
  138.                 c.close();
  139.                 }
  140.                 }catch(SQLException e){
  141.                         s=e.toString();
  142.                        
  143.                 }
  144.                 //s= c.getString(1);
  145.                 //imie.setText(s);
  146.                 //String st=dodajUsera("Ciapa", "Rapa");
  147.                 wyszukany.setText(s);
  148.                
  149.                
  150.                 //System.out.println("Wynik zapytania po kliknięciu ");
  151.                 return s;
  152.         }
  153.        
  154.        
  155.        
  156.         SQLiteDatabase  dodajUsera (String im, String nazw){
  157.                 //opener.onOpen(dbTest);
  158.                 //opener.getWritableDatabase();
  159.                 String query = "INSERT INTO USER (IMIE, NAZWISKO) VALUES ('";
  160.                 StringBuffer sbuf = null;
  161.                 sbuf=new StringBuffer(query);
  162.                 //sbuf.insert(0, "");
  163.                 sbuf.append(im);
  164.                 sbuf.append("','");
  165.                 sbuf.append(nazw);
  166.                 sbuf.append("');commit;");
  167.                 String ss=sbuf.toString();
  168.                 try{
  169.                 dbTest.execSQL(ss);
  170.                 }catch(SQLException e){
  171.                         imie.setText(e.toString());
  172.                 }
  173.                 //dbTest.
  174.                 return dbTest;
  175.         }
  176.        
  177.         @Override
  178.         public boolean onCreateOptionsMenu(Menu menu) {
  179.                 // Inflate the menu; this adds items to the action bar if it is present.
  180.                 getMenuInflater().inflate(R.menu.baza, menu);
  181.                 return true;
  182.         }
  183.  
  184.        
  185.         @Override
  186.         public boolean onOptionsItemSelected(MenuItem item) {
  187.                 // Handle action bar item clicks here. The action bar will
  188.                 // automatically handle clicks on the Home/Up button, so long
  189.                 // as you specify a parent activity in AndroidManifest.xml.
  190.                 int id = item.getItemId();
  191.                 if (id == R.id.action_settings) {
  192.                         return true;
  193.                 }
  194.                 return super.onOptionsItemSelected(item);
  195.         }
  196. }
  197.