2016-06-14 22 views
0

ボタンをクリックしたときに現在の日付値を保存しようとしています.... DBAdapterをsqliteに保存するためにDBAdapterを使用していますが、DBAdapterの例のように2つのフィールド6つのフィールドのためにこれdoesnt仕事。私の問題はどこですか?DBAdapterクラスにフィールドを追加

DBAdapter.java

import java.io.StringReader; 
import java.util.ArrayList; 
import javax.xml.parsers.SAXParser; 
import javax.xml.parsers.SAXParserFactory; 
import org.xml.sax.InputSource; 
import org.xml.sax.XMLReader; 
import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 

public class DBAdapter { 

private static final String TAG = "DBAdapter"; //used for logging database version changes 

// Field Names: 
public static final String KEY_ROWID = "_id"; 
public static final String KEY_YEAR = "year"; 
public static final String KEY_MONTH = "month"; 
public static final String KEY_DAY = "day"; 
public static final String KEY_HOUR = "hour"; 
public static final String KEY_MINUTE = "minute"; 
public static final String KEY_SECOND = "second"; 


public static final String[] ALL_KEYS = new String[] { KEY_ROWID, KEY_YEAR, KEY_MONTH, KEY_DAY, KEY_HOUR, KEY_MINUTE, KEY_SECOND}; 

// Column Numbers for each Field Name: 
public static final int COL_ROWID = 0; 
public static final int COL_TASK = 1; 
public static final int COL_DATE = 2; 

// DataBase info: 
public static final String DATABASE_NAME = "dbToDo12"; 
public static final String DATABASE_TABLE = "mainToDo12"; 
public static final int DATABASE_VERSION = 2; // The version number must be incremented each time a change to DB structure occurs. 

//SQL statement to create database 
private static final String DATABASE_CREATE_SQL = 
     "CREATE TABLE " + DATABASE_TABLE 
       + " (" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " 
       + KEY_YEAR + " TEXT NOT NULL, " 
       + KEY_MONTH + " TEXT" 
       + KEY_DAY + " TEXT" 
       + KEY_HOUR + " TEXT" 
       + KEY_MINUTE + " TEXT" 
       + KEY_SECOND + " TEXT" 
       + ");"; 

private final Context context; 
private DatabaseHelper myDBHelper; 
private SQLiteDatabase db; 


public DBAdapter(Context ctx) { 
    this.context = ctx; 
    myDBHelper = new DatabaseHelper(context); 
} 

// Open the database connection. 
public DBAdapter open() { 
    db = myDBHelper.getWritableDatabase(); 
    return this; 
} 

// Close the database connection. 
public void close() { 
    myDBHelper.close(); 
} 

// Add a new set of values to be inserted into the database. 
public long insertRow(String year, String month,String day, String hour,String minute,String second) { 
    ContentValues initialValues = new ContentValues(); 
    initialValues.put(KEY_YEAR, year); 
    initialValues.put(KEY_MONTH, month); 
    initialValues.put(KEY_DAY, day); 
    initialValues.put(KEY_HOUR, hour); 
    initialValues.put(KEY_MINUTE, minute); 
    initialValues.put(KEY_SECOND, second); 

    // Insert the data into the database. 
    return db.insert(DATABASE_TABLE, null, initialValues); 
} 

// Delete a row from the database, by rowId (primary key) 
public boolean deleteRow(long rowId) { 
    String where = KEY_ROWID + "=" + rowId; 
    return db.delete(DATABASE_TABLE, where, null) != 0; 
} 

public void deleteAll() { 
    Cursor c = getAllRows(); 
    long rowId = c.getColumnIndexOrThrow(KEY_ROWID); 
    if (c.moveToFirst()) { 
     do { 
      deleteRow(c.getLong((int) rowId)); 
     } while (c.moveToNext()); 
    } 
    c.close(); 
} 

// Return all data in the database. 
public Cursor getAllRows() { 
    String where = null; 
    Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS, where, null, null, null, null, null); 
    if (c != null) { 
     c.moveToFirst(); 
    } 
    return c; 
} 

// Get a specific row (by rowId) 
public Cursor getRow(long rowId) { 
    String where = KEY_ROWID + "=" + rowId; 
    Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS, 
      where, null, null, null, null, null); 
    if (c != null) { 
     c.moveToFirst(); 
    } 
    return c; 
} 

// Change an existing row to be equal to new data. 
public boolean updateRow(long rowId, String year, String month,long day, String hour, String minute,long second) { 
    String where = KEY_ROWID + "=" + rowId; 
    ContentValues newValues = new ContentValues(); 
    newValues.put(KEY_YEAR, year); 
    newValues.put(KEY_MONTH, month); 
    newValues.put(KEY_DAY, day); 
    newValues.put(KEY_HOUR, hour); 
    newValues.put(KEY_YEAR, year); 
    newValues.put(KEY_MINUTE, minute); 
    newValues.put(KEY_SECOND, second); 

    // Insert it into the database. 
    return db.update(DATABASE_TABLE, newValues, where, null) != 0; 
} 


private static class DatabaseHelper extends SQLiteOpenHelper 
{ 
    DatabaseHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase _db) { 
     _db.execSQL(DATABASE_CREATE_SQL); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) { 
     Log.w(TAG, "Upgrading application's database from version " + oldVersion 
       + " to " + newVersion + ", which will destroy all old data!"); 

     // Destroy old database: 
     _db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE); 

     // Recreate new database: 
     onCreate(_db); 
    } 
} 

}

MainActivity.java

import android.content.Context; 
import android.database.Cursor; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.widget.Button; 
import android.widget.Toast; 
import android.view.ViewGroup; 
import android.widget.LinearLayout; 
import android.view.View; 
import java.util.Calendar; 

public class MainActivity extends AppCompatActivity { 
    Button plus; 


int second,minute,houre,day,mounth,year; 


DBAdapter mDb = new DBAdapter(this); 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //setContentView(R.layout.activity_main); 

    ViewGroup.LayoutParams superparams = 
      new LinearLayout.LayoutParams(
        ViewGroup.LayoutParams.WRAP_CONTENT, 
        ViewGroup.LayoutParams.MATCH_PARENT); 
    LinearLayout table = new LinearLayout(this); 
    plus = new Button(this); 
    plus.setText("+"); 
    table.addView(plus); 
    setContentView(table); 

    plus.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      Calendar c = Calendar.getInstance(); 
      second = c.get(Calendar.SECOND); 
      minute = c.get(Calendar.MINUTE); 
      houre = c.get(Calendar.HOUR); 
      day = c.get(Calendar.DAY_OF_MONTH); 
      mounth = c.get(Calendar.MONTH); 
      year = c.get(Calendar.YEAR); 
      mDb.open(); 
      mDb.insertRow(Integer.toString(year), Integer.toString (mounth),Integer.toString(day), Integer.toString(houre),Integer.toString(minute), Integer.toString(second)); 
      mDb.close(); 
      mDb.open(); 
      Cursor g=mDb.getRow(2); 
      DisplayContact(g); 
      mDb.close(); 
     } 
    }); 
} 
    public void DisplayContact(Cursor c) 
{ 
    Toast.makeText(this,"id: "+c.getString(0)+"\n"+ 
        "year: "+c.getString(1)+"\n"+ 
        "month: "+c.getString(2), 
      Toast.LENGTH_LONG).show(); 
} 

}

答えて

0

あなたが不足しているカンマここにあなたの列仕様の,

+ KEY_MONTH + " TEXT" 
+ KEY_DAY + " TEXT" 
+ KEY_HOUR + " TEXT" 
+ KEY_MINUTE + " TEXT" 

不足しているカンマを追加した後、アプリをアンインストールするか、スキーマバージョンをインクリメントしてテーブルを再作成してください。

+0

私のためにあなたの時間を費やしていただきありがとうございます。<3 – farshadto

関連する問題