Search the Community
Showing results for tags 'sqlite'.
-
brief overview of what I want my app to do, it takes in the previous configured wifi information and saves them to a table. I have everything working apart from these two problems: Problem 1: So whenever I try and check if the wifi is enabled on the device, the app will crash. Here is the code for the Wifi problem: public void checkWifiState() { if(!wifi.isWifiEnabled()) { Toast.makeText(getApplicationContext(), "Wifi is disabled, please enable it", Toast.LENGTH_SHORT).show(); wifi.setWifiEnabled(true); } } EDIT: found out that wifi.setWifiEnabled(true); is what is breaking the app for Problem 1. Problem 2: I want to drop my table and recreate it, whenever the user trys to press the button to check previous connections. The reason for this is becuase I don't want duplicate data in the database. here is the code for the SQLite problem: this is in a seperate java file called DatabaseHelper that extends SQLiteOpenHelper public void dropTableAndCreateNew() { SQLiteDatabase db = this.getWritableDatabase(); db.delete(TABLE_NAME,null,null); onCreate(db); } I have tried setting the whereClause to "1" on the delete method. It still crashed when I tried to run it on my phone.
- 2 replies
-
- programming
- java
-
(and 3 more)
Tagged with:
-
Hello guys! I have a PHP script that is intended to plot power over time. I have a database with 10 000+ data points in the form of voltage, current and date stamps. The purpose of the PHP script is to calculate power by multiplying voltage and current and then plotting it with time on the x-axis. Right now i have put voltage in an array and just tried to plot it with a few letters on the x axis. When i try to plot the information i get no error message telling me what is wrong, i only get a small Icon with no information with in it. Is this caused because i have to few data points on the x-axis compared to the y-axis. By right now i have no idea of how i should continue to add the functionality which i require, what would a good way to continue be? I'm open to any suggestions and start over with the code if that is necessary. The code so far: <?php // content="text/plain; charset=utf-8" require_once ('/var/www/html/jpgraph/src/jpgraph.php'); require_once ('/var/www/html/jpgraph/src/jpgraph_line.php'); class MyDB extends SQLite3 { function __construct() { $this->open('/home/pi/Documents/Projects/ArduinoSerialImport/solartracker.db'); } } $db = new MyDB(); if(!$db){ echo $db->lastErrorMsg(); } else { echo "Opened database successfully\n"; } $sql =<<<EOF SELECT voltage from stuffToPlot; EOF; $voltage = array(); $ret = $db->query($sql); while ($row['voltage'] = $ret->fetchArray(SQLITE3_ASSOC)){ $voltage[] = $row['voltage']; //print_r ($voltage); echo '<pre>'; //var_dump($row); //$isScalar = is_scalar($voltage); //var_dump(isScalar); } $graph = new Graph(500,500); $graph->SetScale('textlin',-10,10,0,0); $theme_class=new UniversalTheme; $graph->SetTheme($theme_class); $graph->img->SetAntiAliasing(false); $graph->title->Set('Filled Y-grid'); $graph->SetBox(false); $graph->img->SetAntiAliasing(); $graph->yaxis->HideZeroLabel(); $graph->yaxis->HideLine(false); $graph->yaxis->HideTicks(false,false); $graph->xgrid->Show(); $graph->xgrid->SetLineStyle("solid"); $graph->xaxis->SetTickLabels(array('A','B','C','D')); $graph->xgrid->SetColor('#E3E3E3'); $p1 = new LinePlot($voltage); $graph->Add($p1); $p1->SetColor("#6495ED"); $p1->SetLegend('Voltage'); $graph->legend->SetFrameWeight(1); // Output line $graph->Stroke(); ?> Thanks a lot in advance! /J
-
I am trying to create an application that would store 'to do works' and show it to the user. I have used SQLiteDatabase. The problem is that, I have setup cardview that would display the data user has entered. How it is displaying? It is supposed to read the data from sqlite database and then display to the card view. I am have been busting my a** off trying to find the solution but I could not. WorkListAdapter.kt package com.minutecodes.worklist.Data import android.content.Context import android.support.v7.widget.RecyclerView import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.TextView import com.minutecodes.worklist.CommonLibs.Work import com.minutecodes.worklist.R class WorkListAdapter(private val workList: ArrayList<Work>, private val context: Context): RecyclerView.Adapter<WorkListAdapter.ViewHolder>() { override fun onCreateViewHolder(parent: ViewGroup, position: Int): ViewHolder { var view = LayoutInflater.from(context).inflate(R.layout.work_list, parent, false) return ViewHolder(view) } override fun getItemCount(): Int { return workList.size } override fun onBindViewHolder(parent: ViewHolder, position: Int) { parent.bindItems(workList[position]) } class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) { var workName = itemView.findViewById(R.id.listWorkNameID) as TextView var assignedTo = itemView.findViewById(R.id.listAssignedToID) as TextView var assignedBy = itemView.findViewById(R.id.listAssignedByID) as TextView var assignedDate = itemView.findViewById(R.id.listDateAssignedID) as TextView fun bindItems(work: Work) { workName.text = work.workName assignedTo.text = work.assignedTo assignedBy.text = work.assignedBy assignedDate.text = work.dateAssigned.toString() } } } DatabaseHandler.kt package com.minutecodes.worklist.Data 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 import com.minutecodes.worklist.CommonLibs.* import java.text.DateFormat import java.util.* import kotlin.collections.ArrayList class DataBaseHandler(context: Context): SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION) { override fun onCreate(db: SQLiteDatabase?) { var DB_TABLE = "CREATE TABLE " +TABLE_NAME+ "(" +KEY_ID+ " INTEGER PRIMARY KEY," + KEY_WORK_NAME + " TEXT," + KEY_ASSIGNED_TO + " TEXT," + KEY_ASSIGNED_BY + " TEXT," + KEY_DATE_ASSIGNED + " LONG" + ")" db?.execSQL(DB_TABLE) } override fun onUpgrade(db: SQLiteDatabase?, p1: Int, p2: Int) { db?.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME) onCreate(db) } //Implementing CRUD functions fun createWork(work: Work) { var db: SQLiteDatabase = writableDatabase var values = ContentValues() //hashmap type values.put(TABLE_NAME, work.workName) values.put(KEY_ASSIGNED_TO, work.assignedTo) values.put(KEY_ASSIGNED_BY, work.assignedBy) values.put(KEY_DATE_ASSIGNED, work.dateAssigned) db.insert(TABLE_NAME, null, values) Log.d("DATA INSERTION", "SUCCESS") db.close() } fun readAWork(id: Int): Work { var db: SQLiteDatabase = writableDatabase val cursor = db.query(TABLE_NAME, arrayOf(KEY_ID, KEY_WORK_NAME, KEY_ASSIGNED_TO, KEY_ASSIGNED_BY, KEY_DATE_ASSIGNED), KEY_ID + "=?", arrayOf(id.toString()), null, null, null, null) if (cursor != null) cursor.moveToFirst() var work = Work() work.workName = cursor.getString(cursor.getColumnIndex(KEY_WORK_NAME)) work.assignedTo = cursor.getString(cursor.getColumnIndex(KEY_ASSIGNED_TO)) work.assignedBy = cursor.getString(cursor.getColumnIndex(KEY_ASSIGNED_BY)) work.dateAssigned = cursor.getLong(cursor.getColumnIndex(KEY_DATE_ASSIGNED)) var dateFormat: java.text.DateFormat = DateFormat.getDateInstance() var formatDate = dateFormat.format(Date(cursor.getLong(cursor.getColumnIndex(KEY_DATE_ASSIGNED))).time) return work } fun readBulkWorks(): ArrayList<Work> { var db: SQLiteDatabase = readableDatabase var list: ArrayList<Work> = ArrayList() var selectAll = "SELECT * FROM " + TABLE_NAME var cursor: Cursor = db.rawQuery(selectAll, null) if (cursor.moveToFirst()) { do { var work = Work() work.workID = cursor.getInt(cursor.getColumnIndex(KEY_ID)) work.assignedTo = cursor.getString(cursor.getColumnIndex(KEY_ASSIGNED_TO)) work.assignedBy = cursor.getString(cursor.getColumnIndex(KEY_ASSIGNED_BY)) work.dateAssigned = cursor.getLong(cursor.getColumnIndex(KEY_DATE_ASSIGNED)) list.add(work) } while (cursor.moveToNext()) } return list } fun updateWork(work: Work): Int { var db: SQLiteDatabase = writableDatabase var values = ContentValues() values.put(KEY_WORK_NAME, work.workName) values.put(KEY_ASSIGNED_TO, work.assignedTo) values.put(KEY_ASSIGNED_BY, work.assignedBy) values.put(KEY_DATE_ASSIGNED, System.currentTimeMillis()) return db.update(TABLE_NAME, values, KEY_ID + "=?", arrayOf(work.workID.toString())) } fun deleteWork(work: Work) { var db: SQLiteDatabase = writableDatabase db.delete(TABLE_NAME, KEY_ID + "=?", arrayOf(work.workID.toString())) db.close() } fun workCount(): Int { var db: SQLiteDatabase = writableDatabase var countQuery = "SELECT * TABLE " + TABLE_NAME var cursor: Cursor = db.rawQuery(countQuery, null) return cursor.count } } WorkActivity.kt package com.minutecodes.worklist.UI import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.support.v7.widget.LinearLayoutManager import android.support.v7.widget.RecyclerView import com.minutecodes.worklist.CommonLibs.Work import com.minutecodes.worklist.Data.DataBaseHandler import com.minutecodes.worklist.Data.WorkListAdapter import com.minutecodes.worklist.R import kotlinx.android.synthetic.main.activity_work.* class WorkActivity : AppCompatActivity() { private var dbHandler: DataBaseHandler? = null private var adapter: WorkListAdapter? = null private var layoutManager: RecyclerView.LayoutManager? = null private var workList: ArrayList<Work>? = null private var workListItems: ArrayList<Work>? = null //for storing bulk works list override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_work) //Initializing variables dbHandler = DataBaseHandler(this) layoutManager = LinearLayoutManager(this) workList = ArrayList<Work>() workListItems = ArrayList<Work>() adapter = WorkListAdapter(workListItems!!, this) recyclerViewID.adapter = adapter recyclerViewID.layoutManager = layoutManager workList = dbHandler!!.readBulkWorks() for(workReader in workList!!.iterator()) { var work = Work() work.workName = workReader.workName work.assignedTo = workReader.assignedTo work.assignedBy = workReader.assignedBy work.dateAssigned = workReader.dateAssigned work.workID = workReader.workID workListItems!!.add(work) } adapter!!.notifyDataSetChanged() } } Main activity: package com.minutecodes.worklist.Activity import android.content.Intent import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.text.TextUtils import android.widget.Toast import com.minutecodes.worklist.CommonLibs.Work import com.minutecodes.worklist.Data.DataBaseHandler import com.minutecodes.worklist.R import com.minutecodes.worklist.UI.WorkActivity import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { private var dbHandler: DataBaseHandler? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setTitle("Add work") dbHandler = DataBaseHandler(this) saveWorkButtonID.setOnClickListener { if(TextUtils.isEmpty(workNameTxtID.text.toString()) || TextUtils.isEmpty(assignedToTxtID.text.toString()) || TextUtils.isEmpty(assignedByTxtID.text.toString())) { Toast.makeText(this, "Enter details", Toast.LENGTH_SHORT).show() } else { var work = Work() work.workName = workNameTxtID.text.toString() work.assignedTo = assignedToTxtID.text.toString() work.assignedBy = assignedByTxtID.text.toString() saveToDB(work) startActivity(Intent(this, WorkActivity::class.java)) } }//end of save button } fun saveToDB(work: Work) { dbHandler!!.createWork(work) } }
-
Hello guys! I am fiddling with some php scripts and i have ran into an issue which i hope someone will be able to assist me with. I'm trying to create a php script which reads data from an sqlite database. The data will be stored in an array and then be plotted using jpgraph. The hurdle that i ran into is "Can not use scalar as an array". From the searching that I've done so far this seems to be related to a badly defined variable. What i find strange is that i have already defined $voltage as an array and i have not been able to find a statement in the code that defines voltage to something other than an array. This might be something that I've missed on my part but i could really use a second pair of eyes to take a look at the code. <?php // content="text/plain; charset=utf-8" require_once ('/var/www/html/jpgraph/src/jpgraph.php'); require_once ('/var/www/html/jpgraph/src/jpgraph_line.php'); class MyDB extends SQLite3 { function __construct() { $this->open('/home/pi/Documents/Projects/ArduinoSerialImport/solartracker.db'); } } $db = new MyDB(); if(!$db){ echo $db->lastErrorMsg(); } else { echo "Opened database successfully\n"; } $sql =<<<EOF SELECT voltage from stuffToPlot; EOF; $voltage = array(); $ret = $db->query($sql); while ($row[0] = $ret->fetchArray(SQLITE3_ASSOC)) { $voltage[] = $row[0]; //var_dump($row); //$isScalar = is_scalar($voltage); //var_dump(isScalar); } $graph = new Graph(300,250); $graph->SetScale("textlin"); $theme_class=new UniversalTheme; $graph->SetTheme($theme_class); $graph->img->SetAntiAliasing(false); $graph->title->Set('Filled Y-grid'); $graph->SetBox(false); $graph->img->SetAntiAliasing(); $graph->yaxis->HideZeroLabel(); $graph->yaxis->HideLine(false); $graph->yaxis->HideTicks(false,false); $graph->xgrid->Show(); $graph->xgrid->SetLineStyle("solid"); $graph->xaxis->SetTickLabels(array('A','B','C','D')); $graph->xgrid->SetColor('#E3E3E3'); $p1 = new LinePlot($voltage); $graph->Add($p1); $p1->SetColor("#6495ED"); $p1->SetLegend('Line 1'); $graph->legend->SetFrameWeight(1); // Output line $graph->Stroke(); ?> Thank you in advance! /Jonathan
- 14 replies
-
The title pretty much says it, I've looked around and seen a few things about the fetchone() method or fetchall() or even the rowcount method but I haven't been able to make it work. The fetchone() method always returns None and the the others behave in similar ways. Does anyone have any idea what Im doing wrong? My current code for the check is this: conn = sqlite3.connect('test.db') c = conn.cursor() full = 0 exist = c.fetchone() if exist is None: full = 0 else: full = 1
-
I recently posted asking about how to use python to interact with SQLite effectively and figured it out with help from the community so I thought I would ask this new question. I have transferred data into an SQLite database, but I need to query that same database with the same program in order to compare new data to the data already in place. This needs to be run repeatedly to check for changes. The problems are that I have one table with that I need to extract data from in chunks of varying size, and compare it with the same chunk from the new data. Another problem is that the rows of each chunk may not have matching data, but overall, in one cell or another, every piece should be there. I simply cant think of a way to check for something like that, not to mention I'm not really sure of the most effective way to query that data from the database to begin with. Any help is appreciated
-
I have code that creates a table and then inserts data into the table. I've set the table to only be created if it doesn't already exist, but how can I do something like that for the insertion of values? For example, my insertion code is this: c.execute("INSERT INTO ip VALUES(?, ?, ?)", (domainA, ip, lastIP)) So how could I modify that to only insert if the table is empty?
-
I have a database in which I store the name, last name, email and password of a user, along with the ID. But I do not know how to get only one field in one row when the user tries to sign in. I need to retrieve the password that's on the same row as the email that's being used to log in. Any help?