jeudi 10 mars 2016

sqlite android error to save the consult

im having a problem programming the aplication,before im programing an aplication with sqlite and the crud works fine but now im have a error ,the aplication save a memory space o other thing but no the words im type in the edittext heres the code

heres the result and the input menu

sqlite data access:

public class Acceso_Datos  extends SQLiteOpenHelper {

public Acceso_Datos(Context ctx){
    super(ctx, "kkk", null, 1); 
}
@Override
public void onCreate(SQLiteDatabase db){

    String  query = "CREATE TABLE persona (" +
            _ID+" INTEGER PRIMARY KEY AUTOINCREMENT," +
            "rut TEXT,"+
            "nombre TEXT,"+
            "apellido TEXT,"+
            "seccion TEXT)";

db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int version_ant, int version_nue){
onCreate(db);
db.execSQL("DROP TABLE EXISTS persona");

 }



//Método para agregar datos
public void insertar(String rut,String nom, String ap,String sec){
abrir();
ContentValues valores  = new ContentValues();
valores.put("rut", rut);
valores.put("nombre", nom);
valores.put("apellido", ap);
valores.put("seccion", sec);
this.getWritableDatabase().insert("persona", null, valores);        
cerrar();
}

public String leer(){
String resultado="";

String filas[] = {_ID, "rut","nombre", "apellido","seccion"};
Cursor c = this.getReadableDatabase().query("persona", filas,null, null,        null, null, null);
int id,rut, nom, ape, sec;
id= c.getColumnIndex(_ID);
rut= c.getColumnIndex("rut");
nom= c.getColumnIndex("nombre");
ape= c.getColumnIndex("apellido");
sec= c.getColumnIndex("seccion");

while(c.moveToNext())
{
    resultado += 
            c.getString(id) +"  "+ 
            c.getString(rut)+"  "+
            c.getString(nom)+"  "+
            c.getString(ape)+"  "+
            c.getString(sec)+"\n";          
}

return resultado;

}

main:

<LinearLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.asistencia.MainActivity" >

<EditText
    android:id="@+id/v1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="21dp"
    android:ems="10" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/v2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="46dp"
    android:ems="10"
    android:inputType="textPersonName" />

<EditText
    android:id="@+id/v3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textPersonName" />

<EditText
    android:id="@+id/v4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="172dp"
        android:layout_marginLeft="28dp"
        android:onClick="ok"
        android:text="Button" />

    <Button
        android:id="@+id/ver"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="32dp"
        android:onClick="ver"
        android:text="Button" />

</LinearLayout>

java main:

package com.example.asistencia;



public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
public void ok(View v){
    Acceso_Datos a=new Acceso_Datos(this);
    EditText v1=(EditText)findViewById(R.id.v1);
    EditText v2=(EditText)findViewById(R.id.v2);
    EditText v3=(EditText)findViewById(R.id.v3);
    EditText v4=(EditText)findViewById(R.id.v4);
    a.insertar(
            v1.toString(),
            v2.toString(), 
            v3.toString(), 
            v4.toString());

}
public void ver(View v){
    Intent m=new Intent(MainActivity.this,Ver.class);
    startActivity(m);
}

}

and the show activity:

Acceso_Datos a=new Acceso_Datos(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_ver);

    TextView tv=(TextView)findViewById(R.id.resultado);
    tv.setText(a.leer().toString());
    }

Aucun commentaire:

Enregistrer un commentaire