如何在相应列下插入数据值?

时间:2013-09-06 03:22:44

标签: android sql sqlite

嗨,我是Android应用程序开发的新手? 我已成功获取数据库以获取和存储我的数据值。 但出于某些原因,当我查看数据库时,它们会相互插入,并且之间有空格。 如何让我输入的值落在相应的列下?

我的数据库Java。

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class LogsDB {

static final String Key_ROWID = "_id";
static final String Key_Date = "Date";
static final String Key_Milkers = "Milkers";
static final String Key_Feeders = "Feeder";
static final String Key_Mastitis = "Mastitis";
static final String Key_Beef = "Beef";

static final String DATABASE_NAME = "LogsDB";
static final String DATABASE_TABLE = "Logs";
static final int DATABASE_VERSION = 1;

private DatabaseHelper myhelper;
private final Context myContext;
private SQLiteDatabase myDatabase;

private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                Key_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "         +
                Key_Date  + " TEXT NOT NULL, " +
                Key_Milkers + " TEXT NOT NULL, " +
                Key_Feeders + " TEXT NOT NULL, " +
                Key_Mastitis + " INTEGER NOT NULL, " +
                Key_Beef + " INTEGER NOT NULL);"
                );
    } 
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
    db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
    onCreate(db);
    }
}
public LogsDB(Context c){
    myContext = c;
}
public LogsDB open() throws SQLException{
    myhelper = new DatabaseHelper(myContext);
    myDatabase = myhelper.getWritableDatabase();
    return this;
}
public void close(){
    myhelper.close();
}
public long creatEntry(String date, String milkers, String feeders,
        String mastitis, String beef) {
    // TODO Auto-generated method stub
    ContentValues cv = new ContentValues();
    cv.put(Key_Date, date);
    cv.put(Key_Milkers, milkers);
    cv.put(Key_Feeders, feeders);
    cv.put(Key_Mastitis, mastitis);
    cv.put(Key_Beef, beef);
    return myDatabase.insert(DATABASE_TABLE, null, cv);

}
public String getData() {
    // TODO Auto-generated method stub
    String[] columns = new String[]{ Key_ROWID, Key_Date, Key_Milkers, Key_Feeders, Key_Mastitis, Key_Beef};
    Cursor c = myDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
    String result = "";

    int iRow = c.getColumnIndex(Key_ROWID);
    int iDate = c.getColumnIndex(Key_Date);
    int iMilkers = c.getColumnIndex(Key_Milkers);
    int iFeeders = c.getColumnIndex(Key_Feeders);
    int iMastitis = c.getColumnIndex(Key_Mastitis);
    int iBeef = c.getColumnIndex(Key_Beef);

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
        result = result  + c.getString(iRow) + " " + c.getString(iDate) + " " + c.getString(iMilkers) + " " +
                c.getString(iFeeders) + " " + c.getString(iMastitis) + " " + c.getString(iBeef) + "\n";

    }
    return result;
}
public String getDate(long l) {
    // TODO Auto-generated method stub
    String[] columns = new String[]{ Key_ROWID, Key_Date, Key_Milkers, Key_Feeders, Key_Mastitis, Key_Beef};
    Cursor c = myDatabase.query(DATABASE_TABLE, columns, Key_ROWID + "=" + l, null, null, null, null);
    if (c !=null);{
        c.moveToFirst();
        String date = c.getString(1);
        return date; 
    }
}
public String getmilkers(long l) {
    // TODO Auto-generated method stub
    String[] columns = new String[]{ Key_ROWID, Key_Date, Key_Milkers, Key_Feeders, Key_Mastitis, Key_Beef};
    Cursor c = myDatabase.query(DATABASE_TABLE, columns, Key_ROWID + "=" + l, null, null, null, null);
    if (c !=null);{
        c.moveToFirst();
        String milkers = c.getString(2);
        return milkers;
}
}   
public String getfeeders(long l) {
    // TODO Auto-generated method stub
    String[] columns = new String[]{ Key_ROWID, Key_Date, Key_Milkers, Key_Feeders, Key_Mastitis, Key_Beef};
    Cursor c = myDatabase.query(DATABASE_TABLE, columns, Key_ROWID + "=" + l, null, null, null, null);
    if (c !=null);{
        c.moveToFirst();
        String feeders = c.getString(3);
        return feeders;
    }
}
public String getmastitis(long l) {
    // TODO Auto-generated method stub
    String[] columns = new String[]{ Key_ROWID, Key_Date, Key_Milkers, Key_Feeders, Key_Mastitis, Key_Beef};
    Cursor c = myDatabase.query(DATABASE_TABLE, columns, Key_ROWID + "=" + l, null, null, null, null);
    if (c !=null);{
        c.moveToFirst();
        String mastitis = c.getString(4);
        return mastitis;
    }
}
public String getbeef(long l) {
    // TODO Auto-generated method stub
    String[] columns = new String[]{ Key_ROWID, Key_Date, Key_Milkers, Key_Feeders, Key_Mastitis, Key_Beef};
    Cursor c = myDatabase.query(DATABASE_TABLE, columns, Key_ROWID + "=" + l, null, null, null, null);
    if (c !=null);{
        c.moveToFirst();
        String beef = c.getString(5);
        return beef;
}

}
public void updateEntry(long lRow, String edate, String emilkers,
        String efeeders, String emastitis, String ebeef) {
    // TODO Auto-generated method stub
    ContentValues Cvedit = new ContentValues();
    Cvedit.put(Key_Date, edate);
    Cvedit.put(Key_Milkers, emilkers);
    Cvedit.put(Key_Feeders, efeeders);
    Cvedit.put(Key_Mastitis, emastitis);
    Cvedit.put(Key_Beef, ebeef);
    myDatabase.update(DATABASE_TABLE, Cvedit, Key_ROWID + "=" + lRow, null);

}
public void deleteEntry(long lRow1) {
    // TODO Auto-generated method stub
    myDatabase.delete(DATABASE_TABLE, Key_ROWID + "=" + lRow1, null);
}
}

我的ViewLogs java。

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Viewlogs extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_viewlogs); 
    TextView tv = (TextView) findViewById(R.id.tvSqlinfo);
    LogsDB info = new LogsDB(this);
    info.open();
    String data = info.getData();
    info.close();
    tv.setText(data);
}
}

我的数据库XML

<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout 
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".Daily_log" >
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/daily_log"
    android:textSize="20sp" />

<EditText
    android:id="@+id/date"
    android:layout_width="258dp"
    android:layout_height="wrap_content"
    android:hint="@string/date" 
    android:inputType="date"/>

<EditText
    android:id="@+id/milkers"
    android:layout_width="258dp"
    android:layout_height="wrap_content"
    android:hint="@string/milkers" />
<EditText
    android:id="@+id/feeders"
    android:layout_width="252dp"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="@string/feeders" />
<EditText
    android:id="@+id/mastitis"
    android:layout_width="252dp"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="@string/mastitis"
    android:inputType="text"/>

<EditText
    android:id="@+id/beef"
    android:layout_width="252dp"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="@string/beef"
    android:inputType="text"/>

<Button
    android:id="@+id/addlog"
    android:layout_width="261dp"
    android:layout_height="wrap_content"
    android:onClick="addlog"
    android:text="@string/add_daily" />

<Button
    android:id="@+id/viewlogs"
    android:layout_width="261dp"
    android:layout_height="wrap_content"
    android:onClick="viewlogs"
    android:text="@string/view_logs"/>

<EditText
    android:id="@+id/finddate"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="date"/>
<Button
    android:id="@+id/info"
    android:layout_width="261dp"
    android:layout_height="wrap_content"
    android:onClick="addlog"
    android:text="@string/get_info" />
<Button
    android:id="@+id/editlog"
    android:layout_width="261dp"
    android:layout_height="wrap_content"
    android:onClick="addlog"
    android:text="@string/edit_log" />
<Button
    android:id="@+id/deletelogs"
    android:layout_width="261dp"
    android:layout_height="wrap_content"
    android:onClick="viewlogs"
    android:text="@string/delete_log"/>
</LinearLayout>
</ScrollView>

我的ViewDatabase Xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TableLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:id="@+id/tablelayout1">
    <TableRow>
        <TextView android:text="Date"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="2"></TextView>
        <TextView android:text="@string/milkers"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="2"></TextView>
        <TextView android:text="@string/feeders"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="2"></TextView>
        <TextView android:text="@string/mastitis"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="2"></TextView>
        <TextView android:text="@string/beef"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="2"></TextView>
    </TableRow>

  </TableLayout>
<TextView android:text="get info from db"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:id="@+id/tvSqlinfo"/>
</LinearLayout>

我的setText java

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Daily_log extends Activity implements OnClickListener {

Button sqladd, sqlview, sqledit, sqlgetinfo, sqldelete;
EditText sqldate, sqlmilkers, sqlfeeders, sqlmastitis, sqlbeef, sqlfinddate;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_daily_log);
     sqladd = (Button) findViewById(R.id.addlog);
     sqldate = (EditText) findViewById(R.id.date);
     sqlmilkers = (EditText) findViewById(R.id.milkers);
     sqlfeeders = (EditText) findViewById(R.id.feeders);
     sqlmastitis = (EditText) findViewById(R.id.mastitis);
     sqlbeef = (EditText) findViewById(R.id.beef);

     sqlview = (Button) findViewById(R.id.viewlogs);
     sqlview.setOnClickListener(this);
     sqladd.setOnClickListener(this);

     sqlfinddate = (EditText) findViewById(R.id.finddate);
     sqlgetinfo = (Button) findViewById(R.id.info);
     sqledit = (Button) findViewById(R.id.editlog);
     sqldelete = (Button) findViewById(R.id.deletelogs);
     sqlgetinfo.setOnClickListener(this);
     sqledit.setOnClickListener(this);
     sqldelete.setOnClickListener(this);

}


@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub

    switch (arg0.getId()) {
    case R.id.addlog:

        boolean diditwork = true;
        try{
        String date = sqldate.getText().toString();
        String milkers = sqlmilkers.getText().toString();
        String feeders = sqlfeeders.getText().toString();
        String mastitis = sqlmastitis.getText().toString();
        String beef = sqlbeef.getText().toString();

        LogsDB entry = new LogsDB(Daily_log.this);
        entry.open();
        entry.creatEntry(date, milkers, feeders, mastitis, beef);
        entry.close();

        }catch (Exception e){
            diditwork = false;
        }finally{
            if (diditwork){
                Dialog d = new Dialog(this);
                d.setTitle("Heck Yea");
                TextView tv = new TextView(this);
                tv.setText("Success");
                d.setContentView(tv);
                d.show();
            }
        }
        break;
    case R.id.viewlogs:
        Intent i = new Intent("ag.access.Viewlogs");
        startActivity(i);
        break;
    case R.id.info:
    String fd  = sqlfinddate.getText().toString();
    long l = Long.parseLong(fd);
    LogsDB ldb = new LogsDB(this);
    ldb.open();
    String returneddate = ldb.getDate(l);
    String returnmilkers = ldb.getmilkers(l);
    String returnfeeders = ldb.getfeeders(l);
    String returnmatitis = ldb.getmastitis(l);
    String returnbeef = ldb.getbeef(l);
    ldb.close();

    sqldate.setText(returneddate);
    sqlmilkers.setText(returnmilkers);
    sqlfeeders.setText(returnfeeders);
    sqlmastitis.setText(returnmatitis);
    sqlbeef.setText(returnbeef);
        break;

    case R.id.editlog:

        String sRow  = sqlfinddate.getText().toString();
        long lRow = Long.parseLong(sRow);
        String edate = sqldate.getText().toString();
        String emilkers = sqlmilkers.getText().toString();
        String efeeders = sqlfeeders.getText().toString();
        String emastitis = sqlmastitis.getText().toString();
        String ebeef = sqlbeef.getText().toString();

        LogsDB ed = new LogsDB(this);
        ed.open();
        ed.updateEntry(lRow, edate, emilkers, efeeders, emastitis, ebeef);
        ed.close();
        break;
    case R.id.deletelogs:
        String dRow  = sqlfinddate.getText().toString();
        long lRow1 = Long.parseLong(dRow);
        LogsDB d = new LogsDB(this);
        d.open();
        d.deleteEntry(lRow1);
        d.close();

        break;

    }
}
}

非常感谢您的帮助。 谢谢

2 个答案:

答案 0 :(得分:0)

看来你的getData方法返回一个由空格分隔的字符串:

 result = result  + c.getString(iRow) + " " + c.getString(iDate) + " " + c.getString(iMilkers) + " " +
            c.getString(iFeeders) + " " + c.getString(iMastitis) + " " + c.getString(iBeef) + "\n";

最好将此作为数组返回,并使用它来显示结果,或者用管道或逗号分隔它:+“|” +或+“,”并将其拆分以显示结果。

我没有看到你的“setText”方法显示结果......

答案 1 :(得分:0)

尝试以下代码在数据库文件中添加 -

ArrayList<HashMap<String, String>> list;
if (c.moveToFirst()) {
        do {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("iRoW", c.getString(0));
            map.put("iDate", c.getString(1));
            map.put("iFeeders", c.getString(2));
            list.add(map);
        } while (c.moveToNext());
    }
    c.close();
    return list;

在文件调用数据库函数中通过以下代码检索:

的ArrayList&LT;的HashMap&GT; retrieveList;    retrieveList.get(位置)获得( “iRow”);